In a PowerShell script, how does one get the value of an environment variable whose name contains parentheses?
To complicate matters, some variables' names contains parentheses while others have similar names without parentheses. For example (using cmd.exe
):
C:\>set | find "ProgramFiles" CommonProgramFiles=C:\Program Files\Common Files CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files ProgramFiles=C:\Program Files ProgramFiles(x86)=C:\Program Files (x86)
We see that %ProgramFiles%
is not the same as %ProgramFiles(x86)%
.
My PowerShell code is failing in a weird way because it's ignoring the part of the environment variable name after the parentheses. Since this happens to match the name of a different, but existing, environment variable I don't fail, I just get the right value of the wrong variable.
Here's a test function in the PowerShell scripting language to illustrate my problem:
function Do-Test { $ok = "C:\Program Files (x86)" # note space between 's' and '(' $bad = "$Env:ProgramFiles" + "(x86)" # uses %ProgramFiles% $d = "${ Env:ProgramFiles(x86) }" # fail (2), LINE 6 # $d = "$Env:ProgramFiles(x86)" # fail (1) if ( $d -eq $ok ) { Write-Output "Pass" } elseif ( $d -eq $bad ) { Write-Output "Fail: (1) %ProgramFiles% used instead of %ProgramFiles(x86)%" } else { Write-Output "Fail: (2) some other reason" } }
And here's the output:
PS> Do-Test Fail: (2) some other reason
Is there a simple change I can make to line 6 above to get the correct value of %ProgramFiles(x86)%?
NOTE: In the text of this post I am using batch file syntax for environment variables as a convenient shorthand. For example %SOME_VARIABLE% means "the value of the environment variable whose name is SOME_VARIABLE". If I knew the properly escaped syntax in PowerShell, I wouldn't need to ask this question.
Windows environment variables are visible as a PS drive called Env: To list all the environment variables use: Get-Childitem env: (or just dir env:)
In this syntax, the dollar sign ( $ ) indicates a variable, and the drive name ( Env: ) indicates an environment variable followed by the variable name ( windir ). The 'Foo' environment variable is set to: An example An example! For more information about variables in PowerShell, see about_Variables.
An environment variable is written as ${VARIABLE}, where VARIABLE may consist of uppercased alphanumeric characters (A-Z, 0-9) and underscore (_). The variable name must start with A-Z.
To set the environmental variable using PowerShell you need to use the assignment operator (=). If the variable already exists then you can use the += operator to append the value, otherwise, a new environment variable will be created.
Simple. Change line 6 to remove the spaces inside the brackets:
$d = "${Env:ProgramFiles(x86)}" # LINE 6 (NO spaces inside brackets)
You just have to wrap the variable that contains () with {}. No spaces inside the brackets.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With