PowerShell: String

By Xah Lee. Date: . Last updated: .

String is a sequence of characters.

Single Quoted String

Character sequence between APOSTROPHE is literal string. (Can contain multiple lines.)

$x = 'some
thing'

echo $x

To include a single quote inside single quoted string, use two single quotes.

$x = 'don''t'
$x -eq "don't"
# backslash is literal
$x = 'a\nb'

Double Quoted String

Character sequence between QUOTATION MARK is expandable string (aka interpreted, interpolated). Variable or expression inside are replaced by their value

$n = 4
$x = "I have $n cats"
$x -eq 'I have 4 cats'

Expression is also expanded.

$x = "I have $(2+1) cats"
$x -eq '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

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.

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

Here-String

PowerShell String

PowerShell

Practical How-To

list files

list dirs

on dir

on file

Windows config

zip tar

misc


PowerShell in Depth

Programing PowerShell

Value Types

String

Variable

Boolean

Conditional

Data Structure

Loop and Iteration

Input/Output

Function

Profile and Script

Script Examples