PowerShell: Double Quoted String

By Xah Lee. Date: . Last updated: .

Double Quoted String

$n = 4
$x = "I have $n cats"
Write-Host $x
# I have 4 cats

Expression is also expanded.

$x = "I have $(2+1) cats"
Write-Host $x

# I have 3 cats

Embed Variable

It should have the form

${name}

or

$name

The curly brackets can be omitted if there is no space after the name, nor some other non-letter character.

$x = 4
"${x}cats"
# 4cats

Embed Expression

It should have the form $(expr).

"$(3+4) cats"
# 7 cats

Syntax for Newline

use literal newline or use `n.

"line1
line2"
$x = "line1`nline2"

Syntax for Literal Dollar Sign

use `$

$x = 4
"`$x cats"
# $x cats

Syntax for QUOTATION MARK

To include a QUOTATION MARK inside a QUOTATION MARK quoted string, precede it with GRAVE ACCENT `

"He said: `"yes`""

# He said: "yes"

Or precede it with QUOTATION MARK.

"He said: ""yes"""

# He said: "yes"

Escape Character

PowerShell, string and regular expression