Question

I have a PowerShell (2.0) script that runs an executable beneath my program files directory, and I'm using an environmental variable to reference the path:

Start-Process "$($env:ProgramFiles)\ProgramFolder\Executable.exe"

This worked fine on my x86 machine, but now I'm trying to run it on an x64 machine. Since the executable is 32-bit, it resides beneath "C:\Program Files (x86)", and therefore I've adjusted my script as follows since the environmental variable I need to use is ProgramFiles(x86):

Start-Process "$($env:ProgramFiles(x86))\ProgramFolder\Executable.exe"

I'm getting this error though:

Unexpected token '(' in expression or statement.

So how do I reference that environmental variable given that it contains parentheses?

Was it helpful?

Solution

You don't need the subexpression $() in this case:

"${env:ProgramFiles(x86)}\ProgramFolder\Executable.exe"

Outputs:

C:\Program Files (x86)\ProgramFolder\Executable.exe

If you still want to use a subexpression, you can specify a variable name that contains PowerShell syntax characters like so:

"$(${env:ProgramFiles(x86)})\ProgramFolder\Executable.exe"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top