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...
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.
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.
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.
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%"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With