![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | How to run PS script with parameters from a batch file How to run PS script with parameters from a batch file Few weeks ago I posted the same question and an easy solution was not found. I have just discovered a very simple way to solve the problem. PROBLEM Quote: I’d like to run a PS script with parameters from a cmd batch file. These parameters are actually batch variables like %1, %2, %MyEnvVar% and etc. The commands: powershell.exe -command .\MyScript.ps1 '%1' '%2' '%MyEnvVar%' powershell.exe -command .\MyScript.ps1 "%1" "%2" "%MyEnvVar%" powershell.exe -command .\MyScript.ps1 "'%1'" "'%2'" "'%MyEnvVar%'" are not good at all if parameter values contains for example `, ' or $. The trick is to pass %1 and %2 values via environment variables. Note that nothing should be quoted or escaped at all: [batch] set arg1=%~1 set arg2=%~2 PowerShell -command .\MyScript.ps1 $env:arg1 $env:arg2 $env:MyEnvVar [/batch] CAVEATS For existing %MyEnvVar% I do not see any caveats. If %1, %2 are regular data like paths or wildcards - I do not see problems too. Please, correct me if I miss something. This way will not work only if %1, %2 contain symbols special for cmd: for example >, &, |. Such data still require another solution. But they look problematic for batch files themselves. -- Thanks, Roman |
My System Specs![]() |
| | #2 (permalink) |
| | Re: How to run PS script with parameters from a batch file You can handle the special cases you note at bottom by escaping those special characters with a preceding "^". Example: C:\temp>echo ^|^>^^^&^< |>^&< I've already done something similar to this, but it was about creating a shortcut, _not_ invoking a powershell script from cmd.exe. The problem you show is actually much more complicated. "Roman Kuzmin" <RomanKuzmin@discussions.microsoft.com> wrote in message news:F749EAFE-2F8C-496A-8D01-B81E9257F0A9@microsoft.com... > How to run PS script with parameters from a batch file > > Few weeks ago I posted the same question and an easy solution was not > found. > I have just discovered a very simple way to solve the problem. > > PROBLEM > > Quote: > I'd like to run a PS script with parameters from a cmd batch file. These > parameters are actually batch variables like %1, %2, %MyEnvVar% and etc. > The > commands: > powershell.exe -command .\MyScript.ps1 '%1' '%2' '%MyEnvVar%' > powershell.exe -command .\MyScript.ps1 "%1" "%2" "%MyEnvVar%" > powershell.exe -command .\MyScript.ps1 "'%1'" "'%2'" "'%MyEnvVar%'" > are not good at all if parameter values contains for example `, ' or $. > > SOLUTION > > The trick is to pass %1 and %2 values via environment variables. Note that > nothing should be quoted or escaped at all: > > [batch] > set arg1=%~1 > set arg2=%~2 > PowerShell -command .\MyScript.ps1 $env:arg1 $env:arg2 $env:MyEnvVar > [/batch] > > CAVEATS > > For existing %MyEnvVar% I do not see any caveats. If %1, %2 are regular > data > like paths or wildcards - I do not see problems too. Please, correct me if > I > miss something. > > This way will not work only if %1, %2 contain symbols special for cmd: for > example >, &, |. Such data still require another solution. But they look > problematic for batch files themselves. > > -- > Thanks, > Roman |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| batch file / script to replace information | VB Script | |||
| How can I play a Wav file from batch/cmd script? | Vista performance & maintenance | |||
| Help with a Batch file script (FTP) | Vista General | |||
| Call powershell script via Batch-File | PowerShell | |||
| How to run PS script with parameters from cmd batch file? | PowerShell | |||