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.
`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 `n123.45`t600 Header1 Header2 123.45 600 PS C:\> "`a `a"
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). 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%'"
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.)
In many PowerShell expressions 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 \\ )
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 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
Related:
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)
© Copyright SS64.com 1999-2013
Some rights reserved