Access Environment Variables
1. Problem
You want to use an environment variable (such as the system path, or current user’s name) in your script or interactive session.
2. Solution
PowerShell offers several ways to access environment variables.
To list all environment variables, list the children of the env drive:
       Get-ChildItem env:
To get an environment variable using a more concise syntax, precede its name with $env:
       $env:variablename
i.e.: $env:username
To get an environment variable using its Provider path, supply env: or Environment:: to the Get-ChildItem cmdlet:
       Get-ChildItem env:variablename
       Get-ChildItem Environment::variablename
3. Discussion
PowerShell provides access to environment variables through its environment provider. Providers let you work with data stores (such as the registry, environment variables, and aliases) much as you would access the filesystem.
By default, PowerShell creates a drive (called env) that works with the environment provider to let you access environment variables. The environment provider lets you access items in the env: drive as you would any other drive: dir env:\variablename or dir env:variablename. If you want to access the provider directly (rather than go through its drive), you can also type dir Environment::variablename.
However, the most common (and easiest) way to work with environment variables is by typing $env:variablename. This works with any provider but is most typically used with environment variables.
This is because the environment provider shares something in common with several other providers—namely support for the *-Content set of core cmdlets (see Example 3-1).
Example 3-1. Working with content on different providers
|
Code View: Scroll / Show All PS >"hello world" > test PS >Get-Content c:test hello world PS >Get-Content variable:ErrorActionPreference Continue PS >Get-Content function:more param([string[]]$paths); if(($paths -ne $null) -and ($paths.length -ne 0)) { ...
      Get-Content $local:file | Out-Host -p } } else { $input | Out-Host ...
PS >Get-Content env:systemroot C:\WINDOWS Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
|
For providers that support the content cmdlets, PowerShell lets you interact with this content through a special variable syntax (see Example 3-2).
Example 3-2. Using PowerShell’s special variable syntax to access content
|
Code View: Scroll / Show All PS >$function:more param([string[]]$paths); if(($paths -ne $null) -and ($paths.length -ne 0)) { ...
      Get-Content $local:file | Out-Host -p } } else { $input | Out-Host ...
PS >$variable:ErrorActionPreference Continue PS >$c:test hello world PS >$env:systemroot C:\WINDOWS Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
|
This variable syntax for content management lets you to both get and set content:
       PS >$function:more = { $input | less.exe }
       PS >$function:more
       $input | less.exe
Now, when it comes to accessing complex provider paths using this method, you’ll quickly run into naming issues (even if the underlying file exists):
       PS >$c:\temp\test.txt
       Unexpected token '\temp\test.txt' in expression or statement.
       At line:1 char:17
       + $c:\temp\test.txt <<<<
The solution to that lies in PowerShell’s escaping support for complex variable names. To define a complex variable name, enclose it in braces:
       PS >${1234123!@#$!@#$12$!@#$@!} = "Crazy Variable!"
       PS >${1234123!@#$!@#$12$!@#$@!}
       Crazy Variable!
       PS >dir variable:\1*
       Name                             Value
       ----                             -----
       1234123!@#$!@#$12$!@#$@!         Crazy Variable!
… and the content equivalent (assuming that the file exists):
       PS >${c:\temp\test.txt}
       hello world
Since environment variable names do not contain special characters, this Get-Content variable syntax is the best (and easiest) way to access environment variables.
Tags: access, enviroment, power shell, variable
