Escape characters, Delimiters and Quotes

The PowerShell escape character is the grave-accent(`)

The escape character can be used in three ways:

1) When used at the end of a line, it is a continuation character - so the command will continue on the next line.

2) To indicate that the next character following should be passed without substitution. For example $myVariable will normally be expanded to display the variables contents but `$myVariable will just be passed as $myVariable

3) When used inside quotation marks, the escape character indicates that the following character should be interpreted as a 'special' character.

Special characters

  `0  Null
  `a  Alert bell/beep
  `b  Backspace
  `f  Form feed
  `n  New line
  `r  Carriage return
  `t  Horizontal tab
  `v  Vertical tab
  `'  Single quote
  `"  Double quote

These special characters are used to format output on the screen. The default tab stop is 8 spaces.
The escaped quotes allow quotation marks to be displayed on screen, rather than being interpreted as the end of a string.

Examples

PS C:\> Write-Host "First Line `nSecond line
First Line
Second Line

PS C:\> Write-Host "Header1`tHeader2 `nItem1`t123.45
 Header1 Header2
 Item1   Item2

PS C:\> "`a `a"

Quotation Marks

Either single or double quotes may be used to specify a literal string: "The world is everlasting"

Double-Quoted Strings (")
When you enclose a string in double quotation marks, any variable names in the string such as "$myVar" will be replaced with the variable's value when the command is processed.

Single-Quoted Strings (')
When you enclose a string in single quotation marks, any variable names in the string such as '$myVar' will appear exacly as typed when the command is processed. Expressions in single-quoted strings are not evaluated.

Often you will want to combine single and double quotes, in the example below the filename *is* expanded, because it is inside the outer double quotes:

"The file is: '$filename_with_spaces' "

$var = 45

"The value of " + '$var' + "is '$var'"
"The value of `$var is '$var'"

'he cried, not loud yet stentorian "Trot Canter Charge"'
"he cried, not loud yet stentorian 'Trot Canter Charge'"
"he cried, not loud yet stentorian ""Trot Canter Charge"""



Here-strings

A here-string is a single-quoted or double-quoted string which can span multiple lines.
Expressions in single-quoted strings are not evaluated.

All the lines in a here-string are interpreted as strings, even though they are not enclosed in quotation marks.

$myHereString = @'
some text with "quotes" and variable names $printthis
some more text
'@


$anotherHereString = @"
The value of `$var is '$var'
some more text
"@

“Be not angry that you cannot make others as you wish them to be, since you cannot make yourself as you wish to be” - Thomas A Kempis

Related:

Pipelines - Pass objects down the pipeline
Variables - Powershell Variables (int, String)



Back to the Top

© Copyright SS64.com 1999-2010
Some rights reserved