Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 987
Alexander MacGregor Avatar asked Jun 24 '11 17:06

Alexander MacGregor


People also ask

How do I run a PowerShell script with spaces in path?

Use & Operator to Deal With Spaces in the Path in PowerShell You can use the & operator to invoke commands by enclosing the path with spaces in double quotes " " . Or, you can also use the . operator to run the command using the double quotes " " in the path.

How do you write file names with spaces in PowerShell?

PowerShell: Use the Grave Accent Character ( ` ) Just add it before each space in the file name. (You'll find this character above the Tab key and below the Esc key on your keyboard.) Each grave accent character tells PowerShell to escape the following character.

What does ampersand mean in PowerShell?

The ampersand tells PowerShell to execute the scriptblock expression. In essence, it's telling PowerShell that the stuff between the curly braces is code and to run it as code. Passing Arguments to Scriptblocks. Like functions, you can pass "parameters" to scriptblocks also known as arguments.


1 Answers

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%"
like image 178
Joel B Fant Avatar answered Sep 25 '22 12:09

Joel B Fant