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

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?

-
For more on this topic, see this Powershell "gotcha"... stackoverflow.com/questions/803521/powershell-pitfalls/… –  Nathan Hartley Apr 21 at 22:30

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"
``````
-
Either use `Invoke-Expression` or `&`, not both. –  Richard Nov 23 '11 at 8:40
More specifically to use `&` with `Invoke-Expression` the `&` needs to be part of the command, something like: `Invoke-Expression '& "\install\sub directory\test.bat"'`. –  Richard Nov 23 '11 at 8:47

The simplest way is to use the invoke operator:

``````&'String containing the path'
``````

Note, to launch an executable `Invoke-Expression` is really the wrong cmdlet, better to use `Start-Process`.

-
Thanks for this answer. I will try Start-Process –  John Hartley Nov 24 '11 at 22:05