# How-to: 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.

Write-Host
"Hello, world"

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 rn Carriage return + New line t Horizontal tab v Vertical tab (use with printer output) The r (carriage return) is ignored in PowerShell (ISE) Integrated Scripting Environment host application console, it does work in a PowerShell console session. 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 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:\> "12t90n1234567890" 12 90 1234567890 PS C:\> Write-Host "Header1tHeader2 n123.45t600" Header1 Header2 123.45 600 PS C:\> "a a"<Two audible beeps are produced.>  ## Command Separator A semicolon ; can be used to split up multiple commands on the same line. First-command ; Second-command ; Third-command which is equivalent to: First-command Second-command Third-command The semicolon Command Separator can be thought of as a short code for a Carriage Return/Newline. It is equivalent to & in the CMD Shell. To have the second command run only if the first fails: Try {Command-One} Catch {Command-Two} ## 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'

While single quotes do not evaluate expressions or variables, they do evaluate wildcards, so the following two expressions both evaluate to True:

"abc" -like "ab*"

'abc' -like 'ab*'

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

If you nest a Single-Quoted String inside a larger Double-Quoted string, the double-quoted rules apply. Similarly if you nest a Double-Quoted String inside a larger Single-Quoted string, the single-quoted rules apply. The outermost quotes always determine the behaviour.

"Every ""cake"" should cost $5" "Every 'cake' should cost $5"
$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 Stop Parsing symbol --% 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"
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 any environment 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) this is the industry-standard regex syntax.
To escape this and treat the backslash as an ordinary character, double it (replace \ with \\ )

PowerShell currently uses CommandLineToArgvW to process command line inputs, Raymond Chen has some examples.

## Here strings

A here string is a single-quoted or double-quoted string which can span multiple lines.
Variables and 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 plain text

and the children's toys
'@

If the above were a single quoted string, the ' in children's would need to be escaped.
If it was a double quoted string, you'd need to escape the instances of " and you'd need to escape the $if you didn't want variable expansion.$anotherHereString = @"
The value of `$var is$var
some more text
"@

Inside a here-string, double and single quotes are not special but quoted literally, all line breaks are preserved.

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

## Delimiters

Delimiters separate one parameter from the next - they split the command line up into words.

The standard delimiter for PowerShell is the space character, in addition the split operator and import-csv both accept options for splitting strings with a choice of delimiter.

“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 PowerShell Cmdlets:

Escaping Distinguished Names using the backslash "\" escape character - Richard L. Mueller.
Using PowerShell to run old command line tools - Jose Barreto.
Invoke-Expression - Run a PowerShell expression.
Join-Path - Combine a path and child-path.
Pipelines - Pass objects down the pipeline.
Variables - PowerShell Variables (int, String).