Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.
If you own the book already, login here to get free, online, searchable access to the entire book's content.
If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".
You want to use an environment variable (such as the system path or the current user’s name) in your script or interactive session.
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
(For example, $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
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).
PS > "hello world" > test PS > Get-Content test hello world PS > Get-Content c:test hello world PS > Get-Content variable:ErrorActionPreference Continue PS > Get-Content function:prompt "PS $($executionContext.SessionState.Path.CurrentLocation) $('>' * ($nestedPromptLevel + 1)) "; (...) 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).
PS > $function:prompt "PS $($executionContext.SessionState.Path.CurrentLocation) $('>' * ($nestedPromptLevel + 1)) "; PS > $variable:ErrorActionPreference Continue PS > $c:test hello world PS > $env:systemroot C:\WINDOWS
This variable syntax for content management lets you both get and set content:
PS > $function:prompt = { "PS > " } PS > $function:prompt "PS > "
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!
The following is 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.
For more information about working with PowerShell variables, see “Variables”. For more information about working with environment variables, type Get-Help About_Environment_Variables
.
“Variables”