Spaces cause split in path with PowerShell
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.
You can escape the space by using single quotations and a backtick before the space:
$path = 'C:\Windows Services\MyService.exe'
$path -replace ' ', '` '
invoke-expression $path
"&'C:\Windows Services\MyService.exe'" | Invoke-Expression
via https://www.vistax64.com/powershell/52905-invoke-expression-exe-has-spaces-its-path.html
Not sure if someone still needs it... I needed to invoke msbuild in powershell and following worked fine:
$MSBuild = "${Env:ProgramFiles(x86)}\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe"
& $MSBuild $PathToSolution /p:OutDir=$OutDirVar /t:Rebuild /p:Configuration=Release
For any file path with space, simply put them in double quotations will work in Windows Powershell. For example, if you want to go to Program Files directory, instead of use
PS C:\> cd Program Files
which will induce error, simply use the following will solve the problem:
PS C:\> cd "Program Files"