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!

Referencing Variables in Strings
In PowerShell, you can use variables in string values. However, the way in which variables are treated at runtime depends on whether you use single or double quotes. When you use single quotes, PowerShell outputs the variable’s name as entered. When you use double quotes, PowerShell outputs the variable’s value.

For example, let’s assign the value eventlog to a string variable named $svc:

$svc = “eventlog”
You can now use that variable in your strings. You just type the variable’s name as you would any other word:
Write-Output "The service is $svc."
When PowerShell sees the dollar sign, it interprets the word as a variable because the string is enclosed in double quotes. PowerShell then inserts the variable’s value in place of its name, giving the result: The service is eventlog.

Now let’s use single quotes:

Write-Output 'The service is $svc.'
This time, PowerShell treats the variable as a literal value and outputs the variable’s name, giving the result: The service is $svc.

If you want to include both the variable’s name and value in a string, you can use double quotes (but not single quotes) and escape the variable you want to keep as a literal value with a backtick (`):

Write-Output `
      "The value of `$svc is $svc."
(Note that the backtick at the end of the first line isn’t being used as an escape character but rather as a continuation character.) The result is: The value of $svc is eventlog.

If you try to use single quotes

Write-Output `
      'The value of `$svc is $svc.'
PowerShell outputs the string as it’s entered, backtick and all: The value of `$svc is $svc.

Using a Variable Alone as an Argument
In the preceding examples, you saw how to include a variable in a string value that was passed as an argument to the Write- Output cmdlet. However, in some cases, you might want to use a variable as a cmdlet argument without it being part of a string. In that case, you can simply use the variable as the argument. For example, the following command uses the $svc variable as an argument for the name of the service: Get-Service $svc If you were to enclose the argument in double quotes, as in

Get-Service "$svc"
you would receive the same result because, as you saw earlier, PowerShell retrieves the variable’s value when it’s enclosed in double quotes. Also as you saw earlier, if the variable appears in single quotes, PowerShell will interpret the variable name literally. As a result, the statement
Get-Service '$svc'
will fail because PowerShell will interpret the service name as '$svc'.

Moving Forward
Variables play a vital role in effective scripting, whether they’re built-in, environment, or user-defined. You’ll find them a useful tool at the command line and in scripts. Be sure to review the PowerShell Help files for more information on variables, and practice creating and using them. Use the Get-Member cmdlet to retrieve methods and properties and try them out. As you move into more complex code, you’ll find that you’ll be using variables—as well as their methods and properties—in a wide range of scripting solutions.

End of Article

   Previous  1  2  3  [4]  Next  


Reader Comments

You must be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? 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
Command Prompt Tricks

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

WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Understanding File-Size Limits on NTFS and FAT

A general confusion about files sizes on FAT seems to stem from FAT32's file-size limit of 4GB and partition-size limit of 2TB. ...


Related Articles How to Handle Long PowerShell Statements

PowerShell Empowerment

PowerShell Script Lets You Check Patches' Status

PowerShell Pointers

Related Events WinConnections and Microsoft® Exchange Connections

Deep Dive into Windows Server 2008 R2 presented by John Savill

PowerShell 201 - eLearning Series with Paul Robichaux

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