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 



PowerShell 101, Lesson 4

How to properly use quotes when working with strings
RSS
Subscribe to Windows IT Pro | See More Systems Administration Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

This statement uses the GetType method of the object’s type, which in this case is String, or more specifically System.String, also shown in Figure 2. For more information about System.String and GetType, see the sidebar “Getting and Using the System.String Object’s Members.”

If you don’t enclose a numerical value in quotes, PowerShell treats the value as a numerical object. For example, the following statement returns an integer object:

Write-Output 123

Again, you can verify the object’s type by using the GetType method:

(Write-Output 123).GetType()

As you can see in Figure 2, the object’s type is Int32.

If a value includes both numbers and letters, PowerShell treats it as a string, whether or not it’s in quotes. For example, the following statement returns a string, as verified by the second statement:

Write-Output 123output
(Write-Output 123output).GetType()

Once again, Figure 2 shows the output of these statements.

In most cases, you can omit quotes if your argument is a string with no embedded spaces. For example, the following three statements use the Set-Location cmdlet to set the working folder to the C drive:

Set-Location C:\  
Set-Location “C:\”
Set-Location ‘C:\’
Now suppose you want to change the working folder to C:\Documents and Settings:
Set-Location `
  C:\Documents and Settings

This statement generates an error because it doesn’t know what to do with the tokens (words) after the first space. As you can see in Figure 3, the parser interprets “and” as a parameter, and because there’s no parameter by this name, the parser generates an error. You can easily fix this problem by enclosing the entire argument in quotes:

Set-Location `
  “C:\Documents and Settings”

Now when you run this statement, it changes the working folder, as shown in Figure 3.

One other important issue when working with strings is how to reference variables within a quoted string. If you enclose a string in double quotes, the variable’s value is used. If you enclose a string in single quotes, the literal value is used. As I mentioned earlier, I’ll cover variables and describe how to reference them in strings in the next lesson. However, if you’re anxious to learn more about using variables in your strings now, refer to the about_quoting_ rules Help file.

Escaping Special Characters in Strings
Up to this point, the arguments you’ve seen in the examples could have taken single or double quotes. As a result, it would appear that there’s no difference between the two. However, there’s one very important difference: Single quotes always treat a string literally, whereas double quotes allow you to escape special characters within the text. A special character is one that, when preceded by a backtick (`), takes a specific action that it would not have taken without the backtick. Table 1 lists PowerShell’s special characters.

The best way to explain this concept is through a few examples. In the following statement, several characters have been escaped in order to change how the text is displayed:

Write-Output (“`n`tText includes” + `
  “`n`t`”escaped`” characters.`n”)

The first escaped character (the one preceded by the first backtick) is n, which in this context inserts a new line. The next escaped character is t, which inserts a tab. 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 (see Lesson 2). In the second line, `n and `t are used several more times. In addition, backticks precede the double quotes surrounding the word escaped. As a result, the double quotes appear in the output. If you refer to Figure 4, you’ll see how the new lines, tabs, and double quotes appear. For more information about escaping characters, see the about_escape_character Help file.

If you try to escape characters in a string enclosed in single quotes, the backtick and special characters have no effect on the output, other than to be treated literally. For example, the statement

Write-Output ‘`tindented`n`twords’

returns the exact string as originally typed, as Figure 4 shows. Note that, in pre-release versions of PowerShell, you could escape characters inside single-quoted strings, and PowerShell would correctly parse them. So, you might come across material that says that this is how PowerShell handles singlequoted strings.

Moving Forward
In most cases, string values play an important role in creating PowerShell statements. The better you understand how to work with strings, the more effective your statements will be. I encourage you to spend time practicing the various ways to use strings. Try enclosing them in single and double quotes, then try running the commands without the quotes. And don’t forget to practice escaping special characters.

End of Article

   Previous  1  [2]  Next  


Reader Comments

You must log on before posting a comment.

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




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?

...


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!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting 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