umbersar
umbersar

Reputation: 1931

running MSBuild command from power shell throws unexpected token in expression or statement error

If i run MSBuild from command prompt, it runs fine:

C:\Users\user1\Documents\testProject> "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" .\Grounded.sln

I am trying to run same from Power Shell but it does not work and throws unexpected token in expression or statement error. What do i need to do:

PS C:\Users\user1\Documents\testProject> "%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" .\Grounded.sln

I guess i have to escape the arguments being passed but can't figure out how.

Upvotes: 0

Views: 2591

Answers (1)

Christian.K
Christian.K

Reputation: 49220

In CMD.EXE the %NAME% is a placeholder for the environment variable NAME's value. So in your case %ProgramFiles(x86)% expands to (usually) C:\Program Files (x86).

In PowerShell, environment variables are not referenced this way. Rather, you'd have to write $env:NAME to reference the value of the variable NAME.

However, there is a complication here. Since the variable name "ProgramFiles(x86)" contains characters which are normally not allowed in a PowerShell variable name, here ( and ), you have to write it like this: ${env:ProgramFiles(x86)}.

Finally, you need to use the call operator &, because you want to treat the string with the full command line as an executable.

So in your case the command line should read this in PowerShell:

& "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe" .\Grounded.sln

Also consider reviewing about_Variables and about_Environment_Variables

Upvotes: 2

Related Questions