Reputation: 125
I have a PowerShell script that installs some stuff. It's erroring where it tries to call an .exe file that has a path with a space in the name:
try
{
cmd /c "C:\Program Files\myfile.exe" -i "C:\myconfig.sql"
}
The error it provides is:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
Why is it tripping up on the space in the file path when I enclose the path in quotes?
Upvotes: 0
Views: 1285
Reputation: 41962
PowerShell will strip outer quotes, as you can see here.
This is the same as why find.exe
doesn't work as expected in PowerShell.
You need to embed the double quotes inside single quotes, or escape the double quote by using `backticks`
or by doubling the double quote:
cmd /c '"C:\Program Files\myfile.exe"' -i '"C:\myconfig.sql"'
cmd /c "`"C:\Program Files\myfile.exe`"" -i "`"C:\myconfig.sql`""
cmd /c "`"C:\Program Files\myfile.exe`"" -i `"C:\myconfig.sql`"
cmd /c """C:\Program Files\myfile.exe""" -i C:\myconfig.sql
You can also use PowerShell 3.0's verbatim arguments symbol:
cmd /c --% "C:\Program Files\myfile.exe" -i "C:\myconfig.sql"
More about quoting rules in PowerShell is here.
However unless you need an internal command of cmd like dir
, for
... you should avoid calling via cmd. Just call the program directly from PowerShell:
try
{
"C:\Program Files\myfile.exe" -i "C:\myconfig.sql"
}
Upvotes: 2