PowerShell Strings
A string is a piece of text. PowerShell strings are .NET System.String objects, so you get PowerShell's own operators and all the .NET string methods.
Single vs double quotes
| Quotes | Name | Variables expanded? | Escape sequences? |
|---|---|---|---|
'…' |
verbatim | no | no (write '' for a single quote) |
"…" |
expandable | yes ($var, $( )) |
yes (`n, `t, `", `$) |
$user = 'Ada'
"Hello, $user"
'Hello, $user'
"Tab:`tafter tab"
"Quote: `"inside`" and a dollar: `$user"
'It''s easy'
"Line one`nLine two"Hello, Ada
Hello, $user
Tab: after tab
Quote: "inside" and a dollar: $user
It's easy
Line one
Line two
PowerShell's escape character is the backtick `, not the backslash. That's handy on Windows, where \ appears in every path. Use single quotes by default and double quotes only when you need expansion.
Here-strings: multi-line text
A here-string starts with @" or @' at the end of a line and ends with "@ or '@ at the start of a line. Everything between them is kept as written, including quotes and blank lines:
$server = 'web01'
$config = @"
[server]
name = "$server"
port = 8080
"@
$config
$template = @'
Use $server here literally, "quotes" and all.
'@
$template[server]
name = "web01"
port = 8080
Use $server here literally, "quotes" and all.
Here-strings are perfect for config files, SQL queries, JSON templates and help text.
Common string methods
$text = ' PowerShell Rocks '
$text.Trim()
$text.Trim().ToUpper()
$text.Trim().Length
$text.Contains('Shell')
$text.Trim().Replace('Rocks', 'Rules')
$text.Trim().IndexOf('Rocks')
$text.Trim().Substring(5, 5)
'a,b,,c'.Split(',')PowerShell Rocks
POWERSHELL ROCKS
16
True
PowerShell Rules
11
Shell
a
b
c
Note that .NET methods like .Contains() and .Replace() are case-sensitive, unlike PowerShell's -like, -match and -replace operators.
Splitting and joining with operators
-split breaks a string into an array using a regular expression; -join glues an array into one string:
$csvLine = 'Oslo;Bergen;Trondheim'
$cities = $csvLine -split ';'
$cities.Count
$cities[1]
'one two three' -split '\s+'
-split ' spaces around words ' # unary form splits on whitespace
($cities | ForEach-Object { $_.ToUpper() }) -join ' | '3
Bergen
one
two
three
spaces
around
words
OSLO | BERGEN | TRONDHEIM
Concatenation and repetition
$first = 'Grace'
$last = 'Hopper'
$first + ' ' + $last
"$first $last"
'=' * 20
[string]::Join('-', 'a', 'b', 'c')
[string]::IsNullOrWhiteSpace(' ')Grace Hopper
Grace Hopper
====================
a-b-c
True
Characters and indexing
A string is a sequence of characters; index it like an array. Negative indexes count from the end:
$word = 'PowerShell'
$word[0]
$word[-1]
$word[0..4] -join ''
$word.ToCharArray().Count
[char[]]'abc' | ForEach-Object { [int]$_ }P
l
Power
10
97
98
99
Tip: Strings are immutable: methods such as
.Replace()return a new string and leave the original unchanged. Assign the result if you want to keep it:$s = $s.Trim().
Test yourself
What is PowerShell's escape character inside double-quoted strings?
- Backslash \
- Backtick `
- Caret ^
- Percent %
Where must the closing "@ of a here-string be?
- Anywhere
- At the start of its own line
- On the same line as the opening @"
- Indented with 4 spaces
What does 'a-b-c' -split '-' return?
- 'abc'
- An array: a, b, c
- 3
- 'a b c'