Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3693018
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:19:44+00:00 2026-05-19T04:19:44+00:00

In a PowerShell script, how does one get the value of an environment variable

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:19:45+00:00Added an answer on May 19, 2026 at 4:19 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've written a Powershell script to delete subfolders within a given folder whose name
Does anyone have a Powershell script to change the credentials used by a Windows
I have a Powershell script that copies files from one location to another. Once
I'm trying to get this simple PowerShell script working, but I think something is
I'm totally new to powershell, and I need some help to get started. What
I've got a nice power-shell driven post-build script that does some magic mumbo-jumbo for
Before one can get started learning PowerShell , one needs to install it and
I have a simple PowerShell script that uses WMI to create a web site
I need to execute a PowerShell script from within C#. The script needs commandline
I wrote a simple batch file as a PowerShell script, and I am getting

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.