Changes

Jump to: navigation, search

Windows PowerShell 1.0 String Quoting and Escape Sequences

4,268 bytes added, 17:12, 14 November 2008
New page: 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 c...
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:

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

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:

<pre>
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
</pre>

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

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

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:

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

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

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

Another problem involves the display of a string which contains double quotes since PowerShell since PowerShell will become confused:

<pre>
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""
</pre>

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'':

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

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

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

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:

<pre>
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''
</pre>

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

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

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.

Navigation menu