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!

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 


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
Friday at PASS Europe 2006

Kevin talks about the closing day of the event and shares a funny Microsoft film. ...

PsExec

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

Escape From Yesterworld

Kevin points you to the funniest SQL Server website ever! ...


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