Windows PowerShell 1.0 String Quoting and Escape Sequences

From Techotopia
Revision as of 21:12, 1 February 2016 by Neil (Talk | contribs) (Text replacement - "<google>BUY_WPS</google>" to "<htmlet>powershell</htmlet>")

Jump to: navigation, search
PreviousTable of ContentsNext
Windows PowerShell 1.0 Commands and AliasesDirecting and Formatting Windows PowerShell 1.0 Output


Purchase and download the full PDF version of this PowerShell eBook for only $8.99


A key requirement 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.


Contents


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 either by 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 double 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

The PowerShell escape character takes the form of a back quote (`) and instructs PowerShell to treat the following character literally, as opposed to interpreting it in some other way. Those familiar with other shells and programming languages will be more familiar with using the back slash character (\) for escaping. The reason for the use of the back quote instead of the backslash is the direct result of the decision in the first version of DOS to use the backslash character to separate the different parts of a file path, for example C:\Windows\Program Files. Other than this difference, the concept of escaping is otherwise the same.

To understand the escape character, consider the first example in this chapter, whereby PowerShell interpreted the second word after the -inputobject paramater as an additional argument:

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

The reason this occurred was because the space character was interpreted not as a space, but as the delimiter between command line tokens. To instruct PowerShell to treat this character as a space, it needs to be preceded by the escape character as follows:

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

Similarly, the escape character can be used to include a $ prefixed word in a double quoted string without it being treated by PowerShell as a variable:

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

Not surprisingly, the escape character can be used to address the problem of including double quotes in a double quoted string:

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

The trick to including a back quote in a string and not have it treated as an escape character is to escape the back quote itself, in other words typing it as ``:

PS C:\Users\Administrator> write-output -inputobject "This is a back quote ``"
This is a back quote `

It is important to note that the back quote is not treated as the escape character when used in a single quoted string. As mentioned previously, everything enclosed in single quotes is treated literally, including the back quote and the special escape sequences which are described in the next section.

PowerShell Special Escape Sequences

Escape sequences involve the use of the back quote escape together with one other character to represent a special character that cannot otherwise be represented in a string. For example, a tab or new line. The following table lists the special escape sequences supported by Windows PowerShell: <google>ADSDAQBOX_FLOW</google>

Escape Sequence

Special Character

`n

New line

`r

Carriage Return

`t

Tab

`a

Alert

`b

Backspace

`"

Double Quote

`'

Single Quote

``

Back Quote

`0

Null

A typical example of the use of a special escape character involves the insertion of a newline into a string using the `n sequence as follows:

PS C:\Users\Administrator> write-output -inputobject "This is line 1`nThis is line 2"
This is line 1
This is line 2

<google>BUY_WPS_BOTTOM</google>