I have a script foo.cmd
:
echo %1 %2
In PowerShell I run:
foo.cmd "a,b" "c"
Expected output: a,b c
Actual output: a b
Why?
I have a script foo.cmd
:
echo %1 %2
In PowerShell I run:
foo.cmd "a,b" "c"
Expected output: a,b c
Actual output: a b
Why?
The double quotes are removed after PowerShell parsed the command line and passes it to CMD for execution, so CMD actually sees a statement
foo.cmd a,b c
Since the comma is one of CMD's delimiter characters that statement is equivalent to
foo.cmd a b c
To avoid this behavior you need to ensure that the double quotes are preserved when passing the arguments to CMD. There are several ways to achieve this. For instance, you could put the double quoted arguments in single qoutes:
foo.cmd '"a,b"' "c"
and change the positional parameters in the batch script to %~1
, %~2
so that CMD removes the double quotes from the arguments.
If you have PowerShell v3 or newer you can use the "magic parameter" --%
to avoid the nested quotes:
foo.cmd --% "a,b" "c"
You still need %~1
and %~2
in the batch script, though.