I have a batch file that runs a powershell script and I need to pass two parameters to the script. In my batch it looks like this:

powershell -executionpolicy remotesigned -File "my script.ps1" "my path1\" "my path2\somefile.txt"

The way it is now, the parameters for my powershell script get completely messed up and I get:

  • my path1" my
  • path2\somefile.txt

I tried encapsulating my params with ' and ` characters and I nearly got what I want, but the second quote character remains in the powershell script. What I get is this:

powershell -executionpolicy remotesigned -File "my script.ps1" "'my path1\'" "'my path2\somefile.txt'"
  • 'my path1\'
  • 'my path2\somefile.txt'

And what I want is this

  • my path1\
  • my path2\somefile.txt

I know I can strip the redundant quotes in the powershell script, but is there a better way to do this? In other words, how can I pass those parameters to the powershell in a way that keeps them sane?

Test powershell script:

echo $args[0]
echo $args[1]
  • I just noticed that the first parameter ends with a \ character which appears to work as an escape character. Now I need to figure out how to work around it. – jahu Mar 28 '15 at 16:42

I managed to figure it out. The problem was the trailing slash in the first parameter. This slash was escaping my quote and because of that it wasn't working as a quote (yet powershell still cut the string at some later space for whatever reason).

My batch file was autogenerated but I managed to fix it by adding another slash at the end of the first parameter (which was expected to end with a slash).

powershell -executionpolicy remotesigned -File "my script.ps1" "my path1\\" "my path2\somefile.txt"

This problem is probably not specific to batch files and is more than likely also affecting running powershell scripts from command line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.