Powershell – Running an executable from PowerShell does not work with spaces in the path

powershell

From the PowerShell prompt:

\Windows\system32\mspaint.exe

will run Paint. So will

Invoke-Expression -command "\Windows\system32\mspaint.exe"

but if there is a space in the path PowerShell spits the dummy e.g.

Invoke-Expression -command "\install\sub directory\test.bat"

Which complains:

The term '\install\sub' is not recognized as the name of a cmdlet, function, script file, or operable program.

What am I missing?

Best Answer

According to this article on Technet, enclosing the path in double quotes is not enough.

The path that you are trying to use must have an & (ampersand) in front of the directory or it shall not work.

For example:

Invoke-Expression -command & "\install\sub directory\test.bat"