Windows PowerShell 1.0 String Quoting and Escape Sequences

From Techotopia
Revision as of 17:15, 14 November 2008 by Admin (Talk | contribs) (Using Double Quotes)

Jump to: navigation, search

A key requirements of any command line shell or scripting language involves gaining an understanding of using quotation marks to encapsulate strings and escape sequences to enter special characters such as newlines or carriage returns. As Windows PowerShell is no exception to this rule, the objective of this chapter is provide a detailed overview of the different techniques for quoting and escaping strings and characters.

Using Double Quotes

Under most circumstances, PowerShell can recognize the type of a value by looking at its contents and position in a command line. For example, PowerShell is able to deduce that hello is a string in the following command:

PS C:\Users\Administrator> write-output -inputobject hello
hello

Unfortunately, if the string passed to the write-output command contains more than one word separated by a space, PowerShell interprets any word after the first one as additional arguments to the command. Since only one argument is associated with the -inputobject parameter, this causes PowerShell some consternation:

PS C:\Users\Administrator> write-output -inputobject hello everyone
Write-Output : A parameter cannot be found that matches parameter name 'everyone'.
At line:1 char:13
+ write-output  <<<< -inputobject hello everyone

This problem can be overcome by enclosing the string in double quotes (") as follows:

PS C:\Users\Administrator> write-output -inputobject "hello everyone"
hello everyone

This solves the problem of confusing PowerShell. A particular advantage (or disadvantage depending on your point of view) of using double quotes is that variables referenced in the double quoted string are expanded to their actual value. For example:

PS C:\Users\Administrator> write-output -inputobject "My favorite color is $mycolor"
My favorite color is red

This can, of course, be a problem if the string is to contain a dollar ($) sign since PowerShell will attempt to interpret this as a variable. For example:

PS C:\Users\Administrator> write-output -inputobject "Your total is $10"
Your total is

Another problem involves the display of a string which contains double quotes.Clearly, PowerShell will have difficulty identifying where the string begins and ends:

PS C:\Users\Administrator> write-output -inputobject "My favorite color is "blue""
Write-Output : A parameter cannot be found that matches parameter name 'blue'.
At line:1 char:13
+ write-output  <<<< -inputobject "My favorite color is "blue""

Both these problems may be alleviated using single quotes or by escaping, both of which are covered in the following sections.

Using Single Quotes

Single quotes work in much the same way as double quotes with one notable difference that variable names are not expanded and the embedding of double quotes in the encapsulated string is allowed. For example, the $mycolor variable is no longer expanded to be red:

PS C:\Users\Administrator> $mycolor="red"
PS C:\Users\Administrator> write-output -inputobject 'My favorite color is $mycolor'
My favorite color is $mycolor

Similarly, the double quotes in the string are displayed literally:

PS C:\Users\Administrator> write-output -inputobject 'My favorite color is "blue"'
My favorite color is "blue"

In fact, anything placed inside single quotes is taken literally, with the expection of other single quotes which cause a problem similar to the use of doublke quotes in a double quoted string. For example:

PS C:\Users\Administrator> write-output -inputobject 'My favorite `color is 'blue''
Write-Output : A parameter cannot be found that matches parameter name 'blue'.
At line:1 char:13
+ write-output  <<<< -inputobject 'My favorite `color is 'blue''

In fact, if single quotes are to be displayed in a string, double quotes must be used to encapsulate the string:

PS C:\Users\Administrator> write-output -inputobject "My favorite `color is 'blue'"
My favorite color is 'blue'

This raises the issue of how to handle a string which contains both single and double quotes. The answer to this problem lies in the use of the escape character.


Using the PowerShell Escape Character

talk about backslashes.