Thursday, March 3, 2011

Environment variables in powershell with a dot(.) in the name

I know I can access environment variables in powershell using $Env. For example, I can access FOO with $Env:FOO

What I can't figure out is how to access the environment variable called FOO.BAR

$Env:FOO.BAR doesn't work. How can I access this from within Powershell?

From stackoverflow
  • Found the answer to my own question:

    Use the .NET method to get the variable:

    [Environment]::GetEnvironmentVariable("FOO.BAR")
    
    Jay Bazuzi : That only works if it's in `env:`.
  • Get-WMIObject Win32_Environment -filter "name='foo.bar'"

  • To access any kind of PowerShell variable where the name contains non-alphanumeric characters, use the ${…} notation as in:

    PS (STA-ISS) (1) > ${env:variable.with.dots} = "Hi there"

    PS (STA-ISS) (2) > ${env:variable.with.dots}

    Hi there

    This works for variables in any drive (registry, filesystem, etc.)

    Jesse Weigert : Thanks! I figured it was something simple!

0 comments:

Post a Comment