Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


June 2008

PowerShell 101, Lesson 5

How to access, create, and use variables
RSS
Subscribe to Windows IT Pro | See More Systems Administration Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

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

   Previous  [1]  2  3  4  Next 


Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Learning Path To read the previous PowerShell 101 lessons, go to
"PowerShell 101, Lesson 1"

"PowerShell 101, Lesson 2"

"PowerShell 101, Lesson 3"

"PowerShell 101, Lesson 4"


If you're beyond the basics, check out
"User-Friendly Time Spans in Windows PowerShell"

"Changing PowerShell's Internal Functions"


Top Viewed ArticlesView all articles
PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...

Microsoft Delivers Service Pack 2 Beta 2 for Vista, Server 2008

Microsoft on Tuesday announced the availability of the Beta 2 version of Service Pack 2 (SP2) for Windows Vista and Windows Server 2008. Since both operating systems were developed from the same code base, they have a common servicing structure and thus ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...


Related Articles How to Handle Long PowerShell Statements

PowerShell Empowerment

PowerShell Script Lets You Check Patches' Status

PowerShell Pointers

Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events SQL Server 2008 – Can You Wait? | Philadelphia

PowerShell 101 - eLearning Series

SQL Server 2008 – Can You Wait? | Atlanta

Check out our list of Free Email Newsletters!

Windows OSs eBooks Understanding and Leveraging Code Signing Technologies

A Guide to Windows Certification and Public Keys

SQL Server Administration for Oracle DBAs

Related Windows OSs Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing