PowerShell: String

By Xah Lee. Date: . Last updated: .

Double Quoted String

Double quoted string is expandable quote, variable inside are expanded.

$n = 4
$x = "there are $n cats"
echo $x
# prints: there are 4 cats

Expression is also expanded.

$x = "there are $(2+1) cats"

To include a newline, use `n.

$x = "A`nB"

To escape a dollar sign inside double quoted string, precede it with GRAVE ACCENT `.

$x = "there are `$n cats"

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

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'

here-string

“here-string” is a syntax to quote literal long 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 / \
"@

In either syntax, the closing quotation mark must be the first character in the line.

String Length

"abc".length

Join String

# join string
$x = "a" + "b"

PowerShell String

PowerShell

How-to

Advanced

Script Examples