Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking powershell command from Windows Explorer 'Send To' menu

I have created a powershell script that will copy selected files to predetermined location.
I have created a batch file to call the Powershell script from Windows Explorer "Send to" menu.
The batch file sends the selected file name as a parameter to the script. The file names can contain spaces. The batch file code is

powershell "C:\copycommand.ps1 '%1'"

When multiple files are selected I want to send all the file names as an array to the powershell script.
I have written the powershell script to accept arrays but I am not able to send the file names as an array from the batch file.

The below batch script sends all the filenames to the batch file but fails when the filename has spaces.

Powershell "C:\copycommand.ps1" %*

Is there a way in which I can send all the filenames to the powershell script.

like image 845
ManojRK Avatar asked Oct 15 '25 14:10

ManojRK


1 Answers

My bat file looks like (the last line is only for debugging, you can remove it):

@echo off
for %%x in (%*) do ( 
    powershell.exe  -Command "C:\copycommand.ps1 '%%x'"
)

powershell.exe  -noexit

My powershell script is:

$args | %{ Write-host "do copy $_"}

Example output is:

do copy C:\Users\Public\Desktop\SRWare Iron.lnk
do copy C:\Users\Public\Desktop\Mozilla Firefox.lnk
do copy C:\Users\Piotrek\Desktop\Windows 7 USB DVD Download Tool.lnk

Updated version

Change bat file to:

@echo off
set myvar=
for %%x in (%*) do call :concat %%x
echo %myvar%
powershell.exe  -Command C:\copycommand.ps1 %myvar%

powershell.exe  -noexit
goto :eof

:concat
set myvar=%myvar% '%1'
goto :eof
like image 131
Piotr Stapp Avatar answered Oct 18 '25 07:10

Piotr Stapp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!