PowerShell: String
Single Quoted String
Single quote is literal. Can contain multiple lines.
$x = 'first line second line' echo $x
To include a single quote inside single quoted string, use two single quotes.
$x = 'don''t'
Double Quoted String
Double quoted string is expandable quote, variable inside are expanded.
$n = 4 $x = "I have $n cats" echo $x # I have 4 cats
Expression is also expanded.
$x = "I have $(2+1) cats"
To include a newline, use `n
.
$x = "A`nB"
Variable Inside Double Quote
It should have the form
${name}
.
The curly bracket can be omitted if there is a space after the name, or some other non-letter character.
$n = 4 "${n}cats" # "4cats"
To make the dollar sign string sequence literal, add a GRAVE ACCENT ` character before it.
$n = 4 "`${n}cats" # result is # ${n}cats
Expression Inside Double Quote
It should have the form
$(expr)
.
"$(3+4)cats" # 7cats
Escape Character
To include a double quote inside a double quoted string, precede it with GRAVE ACCENT `
$x = "He said: `"yes`""
Or precede it with double quote.
$x = "He said: ""yes"""
Here-String
“here-string” (aka heredoc) is a convenient syntax for quoting long multi-lines text. There are two syntaxes.
Use single quote if you want everything literal.
$x = @' long string here. everything is literal variable $n are not intepreted can contain many quotes "" '' tick ` or slashes / \ '@
Use double quote if you want $ variable or expression expanded:
$x = @" long string here may have multiple lines some variable $n cats can contain many quotes "" '' tick ` or slashes / \ "@
The beginning and end quotation syntax must be on their own lines.
String Length
"abc".length
Join String
# join string $x = "a" + "b"