Executive Summary:
Windows PowerShell supports the use of three types of variables: PowerShell built-in variables, Windows environment variables, and user-defined variables. Learn how to access and use the prefilled built-in and prefilled environment variables. Also learn how to create and fill your own variables.
|
Variables are virtual suitcases that you can
use to store and transport data in your Power-
Shell code. Sometimes, the suitcases come prepacked.
For instance, PowerShell’s built-in variables
and Windows environment variables come with
data already assigned to them. Other times, you
need to pack the suitcases yourself. Although these userdefined
variables take a bit more work to use, they contain exactly
what you need. But before you can start using built-in, environment,
and user-defined variables, you need to understand the basics.
Built-In Variables
PowerShell supports many built-in variables that provide such
information as the PowerShell home folder ($PSHOME) and
current working folder ($PWD). To retrieve a list of not only the
built-in but also the user-defined variables available in the current
session, you can run the statement
dir variable: | sort name
This statement starts by using the
dir alias to reference
the Get-ChildItem cmdlet, which takes
variable: as its
argument. This argument is referring to the Variable
drive, one of several drives supported by PowerShell.
As you might have guessed, this drive provides access
to built-in and user-defined PowerShell variables and
their values.
After the Get-ChildItem cmdlet retrieves the variables
and their values, they’re piped to the Sort-Object
cmdlet (represented by the sort alias), which sorts
the output by the variables’ names. If you don’t sort
by name, the variables will be displayed in the order
they’re retrieved.
Figure 1 shows part of a sorted variable list. Such
a list can prove invaluable as long as you’re aware of
an important caveat: When you reference built-in and
user-defined variables in your PowerShell code, you
typically need to precede the variable name with a dollar
sign ($). Unfortunately, this list doesn’t include the
dollar signs in the names. The dollar sign makes it easy
to distinguish variables from other code elements. For
example, the dollar sign lets you easily distinguish between the pwd
alias and the $PWD built-in variable. There are a few cases in which
you don’t precede the variable name with a dollar sign, which I’ll
discuss later.
If you want to retrieve the value of just one built-in variable, you
can simply enter the variable’s name. For example, if you run
$pshome
you’ll receive the path to the PowerShell home folder.
As with any variable in PowerShell, you can use built-in variables
in statements. For example, the following statement uses the
$PSHOME variable to retrieve a list of the .dll files in the PowerShell
home folder:
dir $pshome -filter *.dll
PowerShell supports two types of built-in variables: preference
(e.g., $MaximumErrorCount) and automatic (e.g., $PSHOME).
You can change the values of preference
variables but not automatic variables. If
you try to modify an automatic variable’s
value, you’ll receive an error. For a list of
preference variables, see the about_preference_
variables Help file. (If this file isn’t
available on your system, you can view it
at technet.microsoft.com/en-us/library/
bb978529.aspx.) For a list of automatic variables,
see the about_automatic_variable
Help file. You can find additional information
about built-in variables by viewing the
about_shell_variable Help file.
To change the value of a preference
variable, you simply use the assignment
(=) operator to assign a new value. Take,
for example, the preference variable of
$MaximumErrorCount, which specifies
how many errors to save in the error history
log for the current session. If you want
to change this variable’s value from the
default of 256 to 260, you’d run
$maximumerrorcount = 260
The new value is used until you reassign a
value or end your session.
Environment Variables
In PowerShell, you use the Env drive to
access Windows environment variables.
For example, the following statement uses
this drive to retrieve the Windows environment
variables and their values, then uses
the Sort-Object cmdlet to sort the output by
the variables’ names:
dir env: | sort name
Like the built-in and user-defined variable list, the environment variable list doesn’t
show the prefix you need to include when
you reference environment variables in
PowerShell code. However, unlike builtin
and user-defined variables, which you
preface with $, you need to preface an
environment variable’s name with $env:
when you reference it. For example, the
following statement retrieves the windir
environment variable’s value, which is the
path to the Windows home folder:
$env:windir
To retrieve a list of the .dll files in the Windows
home folder, you’d run
dir $env:windir -filter *.dll
You can change the values of environment
variables. For example, suppose you
want to change the HOMEPATH environment
variable’s value from
\Documents
and Settings\administrator to \Documents
and Settings\administrator\home. You can
use the + operator discussed in Lesson 3
to append the string \home to the HOMEPATH
environment variable’s value:
$env:homepath =
$env:homepath + "\home"
The value stays in effect until you reassign
a new value or end the session. For more
information about environment variables,
see the about_environment_variables Help
file.
Continue on Page 2