0

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?

1
  • 2
    cmd /c "C:\Program Files\myfile.exe" -i "C:\myconfig.sql" -> & "C:\Program Files\myfile.exe" -i "C:\myconfig.sql" Commented Sep 28, 2017 at 14:53

1 Answer 1

2

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"
}
Sign up to request clarification or add additional context in comments.

3 Comments

Testing this now!
Strange, I tried the first suggestion of yours, cmd /c '"C:\Program Files\myfile.exe"' -i '"C:\myconfig.sql"' and got the same error...
Just tried it with triple quotes and got the same error as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.