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!

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 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
CES 2009: Ballmer Announces Windows 7, Windows Live, Live Search Milestones

During his first-ever Consumer Electronics Show (CES) 2009 keynote address last night in Las Vegas, Microsoft CEO Steve Ballmer announced the pending public availability of a feature-complete Windows 7, the final version of Windows Live Essentials, and ...

Command Prompt Tricks

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

Where is Microsoft NetMeeting in Windows XP?

...


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 Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

PowerShell 201 - eLearning Series with Paul Robichaux

Cloud Computing Forum: Integrating Software, Server and Storage as a Service into Your Enterprise IT Delivery Model

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 © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing