Changes

Windows PowerShell 1.0 String Quoting and Escape Sequences

3,663 bytes added, 18:39, 14 November 2008
Using the PowerShell Escape Character
== Using the PowerShell Escape Character ==
talk about backslashesThe 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, but 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: <pre>PS C:\Users\Administrator> write-output -inputobject hello everyoneWrite-Output : A parameter cannot be found that matches parameter name 'everyone'.At line:1 char:13+ write-output <<<< -inputobject hello everyone</pre> 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: <pre>PS C:\Users\Administrator> write-output -inputobject hello` everyonehello everyone</pre> 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: <pre>PS C:\Users\Administrator> write-output -inputobject "Your total is `$10"Your total is $10</pre> Not surprisingly, the escape character can be used to address the problem of including double quotes in a double quoted string: <pre>PS C:\Users\Administrator> write-output -inputobject "My favorite `"color`" is blue"My favorite "color" is blue</pre> 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 was ``: <pre>PS C:\Users\Administrator> write-output -inputobject "This is a back quote ``"This is a back quote `</pre> 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 are 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: <table border="1" cellpadding="5" cellspacing="0" id="E3B" style="border-collapse: collapse; border-color:#cccccc; border-style: solid; border-width: 1px; margin-bottom:20px"><tr bgcolor="#cccccc" style="color:black" valign="top"><th><p>Escape Sequence</p></th><th><p>Special Character</p></th></tr> <tr><td>`n</td><td>New line</td></tr> <tr bgcolor="#e9e9e6"><td>`r</td><td>Carriage Return</td></tr> <tr><td>`t</td><td>Tab</td></tr> <tr bgcolor="#e9e9e6"><td>`a</td><td>Alert</td></tr> <tr><td>`b</td><td>Backspace</td></tr> <tr bgcolor="#e9e9e6"><td>`"</td><td>Double Quote</td></tr> <tr><td>`'</td><td>Single Quote</td></tr> <tr bgcolor="#e9e9e6"><td>``</td><td>Back Quote</td></tr> <tr><td>`0</td><td>Null</td></tr></table>