Powershell – Spaces cause split in path with PowerShell

powershell

I'm having an issue with powershell when invoking an exe at a path containing spaces.

PS C:\Windows Services> invoke-expression "C:\Windows Services\MyService.exe"

The term 'C:\Windows' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

It seems to be splitting on the space between 'Windows' and 'Services'. Any idea how to get around this problem?

Best Answer

Would this do what you want?:

& "C:\Windows Services\MyService.exe"

Use &, the call operator, to invoke commands whose names or paths are stored in quoted strings and/or are referenced via variables, as in the accepted answer. Invoke-Expression is not only the wrong tool to use in this particular case, it should generally be avoided.