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 double quotation marks, the escape character indicates that the following character should be interpreted as a 'special' character.

Special characters

Special characters are used to format/position string output.

   `0  Null
   `a  Alert bell/beep
   `b  Backspace
   `f  Form feed (use with printer output)
   `n  New line
   `r  Carriage return
 `r`n  Carriage return + New line
   `t  Horizontal tab
   `v  Vertical tab (use with printer output)

Using the Escape character to avoid special meaning.

   ``  To avoid using a Grave-accent as the escape character
   `#  To avoid using # to create a comment
   `'  To avoid using ' to delimit a string
   `"  To avoid using " to delimit a string

When setting a string variable the # character does not need to be escaped, but at the command line # will act as a comment unless escaped:

PS C:\> echo 1 # 1

PS C:\> echo 1 `# 1
1
#
1

The escaped quotes allow quotation marks to be displayed on screen rather than being interpreted as the start or end of a string. The same effect can also be achieved by doubling-up the quote marks: "" or ''
For powershell.exe, the horizontal tab stops are every 8th character.

Examples

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

PS C:\> "12`t90`n1234567890"
 12      90
 1234567890

PS C:\> Write-Host "Header1`tHeader2 `n123.45`t600"
 Header1  Header2
 123.45   600

PS C:\> "`a `a"
<Two audible beeps are produced.>

Quotation Marks

Either single or double quotes may be used to specify a literal string.

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, not even escape characters or any of the Special characters listed above. If the string contains any embedded single quotes, they must be doubled (replace with'')

$msg = 'Every "lecture" should cost $5000'

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. You can prevent this substitution by prefixing the $ with an escape character. Any embedded double quotes can be escaped `" or doubled (replace with "")

$msg = "Every ""lecture"" should cost `$5000"
$msg = "Every 'lecture' should cost `$5000"
$var = 45 

"The value of " + '$var' + "is '$var'" 
"The value of `$var is '$var'"
$query = "SELECT * FROM Customers WHERE Name LIKE '%JONES%'"

Verbatim arguments --%

In PowerShell 3.0 the special marker --% is a signal to PowerShell to stop interpreting any remaining characters on the line. This can be used to call a non-PowerShell utility and pass along some quoted parameters exactly as is.

for example instead of escaping every character that PowerShell may interpret:
PS C:\> FIND.EXE '"Search Text"' "C:\Program Files `(x86`)\Demo\text.txt"
we can instead use: 
PS C:\> FIND.EXE --% "Search Text" "C:\Program Files (x86)\Demo\text.txt"

Any PowerShell variables after the --% won’t be expanded but you can work around this by building up the command string using more than one variable:

$command = 'FIND.EXE --%'
$params = "C:\Program Files (x86)\Demo\text.txt"

& $command "Search Text" $params;

If the command type is Application, the parameter --% is not passed to the command. The arguments after --% have anyenvironment variables (strings surrounded by %) automatically expanded. For example: 
PS C:\> FINDSTR.EXE --% "%ProgramFiles%" 
In the above %ProgramFiles% is replaced with the value $env:ProgramFiles

Concatenating Strings

Concatenate strings with + 
In many cases, when combining simple strings, the + operator is not needed: 
PS C:\> $first = "abcde" 
PS C:\> $second = "FGHIJ" 
PS C:\> "$first $second" 
abcde FGHIJ

but when combining more complex objects, the + operator becomes very useful: 
For example if we retrieve an environment variable   
$drive = gci env:SystemDrive 
This returns a DictionaryEntry object with .name and .value properties.

# Adding this to a string will give the wrong result 
PS C:\> "aaa $drive bbb" 
aaa System.Collections.DictionaryEntry bbb

# Concatenating it with a string is closer, but still wrong: 
PS C:\> "aaa " + $drive + " bbb" 
aaa System.Collections.DictionaryEntry bbb

# Specify the .value property and concatenate that with + to get the desired result: 
PS C:\> "aaa " + $drive.value + " bbb" 
aaa C: bbb

# Alternatively use the $( ) SubExpression operator:
PS C:\> "aaa $($drive.value) bbb" 
aaa C: bbb


(an easier method would be using $drive = $env:SystemDrive which will return a system.string in the first place.)

Backslash \

In many programming languages including C# (.Net) and most UNIX shells the escape character is \ 

PowerShell is a Windows shell, and Windows already uses the backslash as a path separator C:\Windows\….

In Windows CMD the escape character is ^ although this was not a great choice as ^ is a valid character in NTFS filenames.

The PowerShell designers could have adopted the forward slash as a path separator C:/Windows/… , and then allocated the backslash as the escape character but this would have caused huge confusion and so instead they left paths unchanged and allocated ` as the escape character.

The backtick ` is a valid character in NTFS filenames, so should you encounter one in a filename, it will need to be escaped.

In some PowerShell expressions (matching operations) a backslash character will be interpreted as the start of a Regular Expression, (e.g. \w = match word)
To escape this and treat the backslash as an ordinary character, double it (replace \ with \\ )

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 
"@

The @ character is also used to create arrays, create hash tables and as a splat operator.

“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