Passing a semicolon as a parameter to a .bat file from both cmd and PowerShell

Viewed 432

This question seems to be rather simple, but even after searching the web for a couple of hours, I was not able to find a solution...

I have a batch file test.bat

set MY_VARIABLE=%~1
echo %MY_VARIABLE%

The point is that I want to call this programm with a semicolon as input parameter, i.e., .\test.bat ";", from both cmd and Windows PowerShell. While this works fine from cmd, PowerShell does not seem to get anything as an input. Is there any way to make this work for both simultaneously?

1 Answers

This is because of command line syntax. The semicolon is one of multiple delimiters, that split the command line into words: a;b would be interpreted as two separate arguments a (%1) and b (%2).

Therefore, quotes are required. Since Powershell uses quotes for string literals (Powershell does its own re-quoting behind the scenes when passing arguments), you need to include them in the string:

.\test.bat '";"'
# or
.\test.bat "`";`""

Or as @mklement0 pointed out, the stop-parsing symbol --% would also be an option:

.\test.bat --% ";"

Note that this is specific to Powershell syntax.

In CMD, this will suffice:

test.bat ";"
Related