Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  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!

User-Defined Variables U
nlike some scripting languages, Power- Shell doesn’t require you to explicitly declare a variable before you use it. You simply assign it a value. For example, the following statement assigns the string one two to the $var1 variable:

$var1 = "one two"

When you define a variable in this way, you’re actually calling the New-Variable cmdlet and providing two arguments to it: the variable’s name and value. For example, the following statement achieves the same results as the preceding statement:

New-Variable var1 "one two"
In this statement, note that the variable’s name isn’t preceded with a dollar sign. When a variable’s name is an argument to the New-Variable cmdlet or to the Set-Variable, Clear-Variable, or Remove-Variable cmdlet (all of which I’ll cover shortly), you don’t include the dollar sign.

One advantage of using the New-Variable cmdlet to create a variable is that you can use other cmdlet parameters. For example, you can use the -option parameter to define the variable as read-only: New-Variable var1 "one two" `

-option ReadOnly
In this case, I’ve specified ReadOnly as an argument to the -option parameter. You can also specify other values as arguments to this parameter. If you specify more than one argument, you need to use commas to separate them. For a complete list of acceptable arguments, see the New-Variable Help file.

When you set a variable as read-only, you can’t change the variable’s value unless you force the change. However, before I get into the details of how to do that, let’s first take a look at how to update a variable. Typically, the simplest way to change a variable’s value is to use the = operator to assign the new value:

$var1 = "three"
You can achieve the same result by using the Set- Variable cmdlet:
Set-Variable var1 "three"
Both these statements will reset the value, but only if the variable is not defined as read-only. If it is read-only, you’ll receive an error and you won’t be able to update the value. The way to work around this issue is to use the -force parameter:
Set-Variable var1 “three” -force
Now the variable will be assigned the new value. As you saw with the New-Variable cmdlet, the advantage to using the Set-Variable cmdlet is that you can use additional parameters.

Now let’s take a look at how to clear a variable’s value. One approach you can take is to set the value to null by using the $null built-in variable:

$var1 = $null
Or, you can use the Clear- Variable cmdlet to achieve the same result:
Clear-Variable var1
However, as before, if the variable is defined as readonly, both these commands will fail. Once again, the workaround is to add the -force parameter:
Clear-Variable var1 `
    -force
Finally, if you want to delete a variable altogether, you can use the Remove-Variable cmdlet:
Remove-Variable var1
Not surprisingly, this command will return an error if the variable is read-only. However, the Remove-Variable cmdlet also supports the -force parameter:
Remove-Variable var1 -force
In most cases, you’ll probably find that you can simply use the = operator to create, change, and clear variables. However, if you want to use the New-Variable, Set-Variable, Clear-Variable, and Remove-Variable cmdlets, you can find more information about them in their respective Help files.

Continue on Page 3

   Previous  1  [2]  3  4  Next 


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
Command Prompt Tricks

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

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...


Related Articles How to Handle Long PowerShell Statements

PowerShell Empowerment

PowerShell Script Lets You Check Patches' Status

PowerShell Pointers

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

PowerShell 201 - eLearning Series with Paul Robichaux

PowerShell 101 - eLearning Series

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 Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


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 DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement