Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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"
share|improve this answer
    
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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.