How to Call CMD.EXE from PowerShell with a Space in the Specified Command's Directory Name How to Call CMD.EXE from PowerShell with a Space in the Specified Command's Directory Name powershell powershell

How to Call CMD.EXE from PowerShell with a Space in the Specified Command's Directory Name


You need to call cmd.exe like this:

cmd.exe /c "`"$_cmd`""

The commands you send to cmd.exe need to be entirely wrapped in their own quotes, not just the paths-with-spaces that are part of those commands. This has to do with how Powershell parses the string and it needs to pass literal quotes to cmd.exe so that it in turn does its own parsing of the contents of double-quotes correctly.

For example, if you were already in a cmd.exe session and set a variable like this:

C:\>set _cmd="C:\Program Files (x86)\VisualSVN Server\bin\svnadmin" verify "C:\My Repositories\App1"

Then simply expanding that variable at the commandline would work:

C:\>%_cmd%

However, if passing it to a new cmd.exe session, it would also need extra quotes:

C:\>cmd.exe /c "%_cmd%"