PowerShell: Double Quoted String, String Expansion

By Xah Lee. Date: . Last updated: .

Double Quoted String, String Expansion

Character sequence between "double quote" is expandable string. (aka interpolated string) Variable or expression inside are replaced by their values.

$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

To include a newline, use literal newline or use `n.

$x = "A`nB"

$y = "A
B"

$x -eq $y

Variable Inside Double Quote

$x = 4
"${x}cats" -eq "4cats" 

To make the dollar sign string sequence literal, add a GRAVE ACCENT ` before it.

$x = 4
"`${x}cats" -eq '${x}cats' 

Expression Inside Double Quote

It should have the form $(expr).

 "$(3+4)cats" -eq '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"""

[see Escape Characters]

PowerShell String