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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jdope
  • 115
  • 2
  • 14
  • 2
    `cmd /c "C:\Program Files\myfile.exe" -i "C:\myconfig.sql"` -> `& "C:\Program Files\myfile.exe" -i "C:\myconfig.sql"` – BenH Sep 28 '17 at 14:53

1 Answers1

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"
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
phuclv
  • 37,963
  • 15
  • 156
  • 475