Powershell – Windows: Escape spaces in command

powershell

I'm trying to execute a simple command with path containing spaces

$solution = "test.sln"
$msbuild = "C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe"
Exec { iex "$msbuild `"$solution`" /t:clean"}
Exec { iex "$msbuild `"$solution`"" }

This throws an error

Exception: The term 'x86' is not recognized as the name

which seems to be because the $msbuild command isn't properly quoted. Tried quoting the command (and escaping quotes with backticks) but still no luck

$msbuild   = "`"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe`""

How can you correctly escape/quote above? What are the escaping rules in Powershell?

Best Answer

$msbuild = "${env:ProgramFiles(x86)}" + '\MSBuild\14.0\Bin\MSBuild.exe'

Try this way. No spaces, so path should be fine.