Problem
You want to use an environment variable (such as the system path, or current user’s name) in your script or interactive session.
Solution
PowerShell offers several ways to access environment variables. To list all environment variables, list the children of the env drive:
GetChildItem 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 GetChildItem cmdlet:
GetChildItem env:variablename
GetChildItem Environment::variablename
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
Example 31. Working with content on different providers
PS >"hello world" > test PS >GetContent c:test hello world PS >GetContent variable:ErrorActionPreference Continue PS >GetContent function:more param([string[]]$paths); if(($paths ne $null) and ($paths.length ne 0)) { ...
GetContent $local:file | OutHost p } } else { $input | OutHost ... PS >GetContent env:systemroot C:\WINDOWS
For providers that support the content cmdlets, PowerShell lets you interact with this content through a special variable syntax
Example 32. Using PowerShell’s special variable syntax to access content
PS >$function:more param([string[]]$paths); if(($paths ne $null) and ($paths.length ne 0)) { …
GetContent $local:file | OutHost p } } else { $input | OutHost … 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 GetContent variable syntax is the best (and easiest) way to access environment variables.