PowerShell: String

By Xah Lee. Date: . Last updated: .

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 literal newline or use `n.

$x = "A`nB"

Variable Inside Double Quote

It should have the form ${name}. The curly brackets can be omitted if there is a space after the name, or some other non-letter character.

$n = 4
# "4cats"

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

$n = 4
# result is
# ${n}cats

Expression Inside Double Quote

It should have the form $(expr).

# 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]


“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.

PowerShell String


Practical How-To

list files

list dirs

on dir

on file

Windows config


PowerShell in Depth

Programing PowerShell

Value Types





Data Structure

Loop and Iteration



Profile and Script

Script Examples