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

1

15

I have the following PowerShell script for verifying my SVN repositories:

$SVNAdminDir = 'C:\Program Files (x86)\VisualSVN Server\bin';
$RepositoryDir = 'C:\My Repositories\App1';
$_cmd = "`"$SVNAdminDir`\svnadmin`" verify `"$RepositoryDir`"";
Write-Host $_cmd; # Copying this into the command prompt runs without an issue...
cmd.exe /c $_cmd; # It's when I try to execute the command from PS that I get the error.

But when I try to execute it, I'm receiving the following error message:

cmd.exe : 'C:\Program' is not recognized as an internal or external command,
At line:5 char:12
+     cmd.exe <<<<  /c $_cmd;
    + CategoryInfo          : NotSpecified: ('C:\Program' is...ternal command,:String) [],     RemoteException
    + FullyQualifiedErrorId : NativeCommandError

operable program or batch file.

Since I'm essentially setting $cmd = '"C:\Program Files (x86)\VisualSVN Server\bin\svnadmin" verify "C:\My Repositories\App1"'; with the double quotes inside of the single quotes, I was expecting the space in C:\Program Files (x86)\... to be passed correctly.

I suspect there's something trivial with the string that I'm missing...

Glynisglynn answered 24/6, 2011 at 17:12 Comment(0)
H
18

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%"
Hypoblast answered 24/6, 2011 at 17:46 Comment(2)
I've not been able to get this to work in PowerShell. cmd.exe continually errors out complaining that 'C:\Program' is not recognized as an internal or external command.... I'm running PowerShell 5.1. Any ideas?Granophyre
so the powershell escape character is backtick?Touchstone

© 2022 - 2024 — McMap. All rights reserved.