Loading
0x60Lesson 7 of 17

Strings and text

Expand variables in strings, build text with -f and here-strings, and split, join and replace with regex.

26 min 7-question quiz 2 code exercises
By the end of this lesson you can
  • Choose between single and double quotes and use $( ) subexpressions
  • Build multi-line text with here-strings and escape characters
  • Transform text with -split, -join, -replace, string methods and Select-String
QuotesExpands $variables and $( )?Use for
'single'no - exactly as writtenfixed text, regexes, anything with $ in it
"double"yesmessages that include values

In double quotes, the backtick ` is the escape character: `n is a newline, `t a tab, `$ a literal dollar sign and `" a quote. (PowerShell uses the backtick because \ is the Windows path separator.) Inside single quotes, write two single quotes to get one: 'Ember''s cave'.

quotes.ps1
1$name = 'Ember'
2$meals = 3
3'Single: $name had $meals meals'
4"Double: $name had $meals meals"
5"Math needs a subexpression: $($meals * 2) half-meals"
6"Tab:`t|  Dollar: `$5  Quote: `"hi`""
7'It''s Ember''s cave'
8@"
9Report for $name
10  meals: $meals
11"@
Output
Single: $name had $meals meals
Double: Ember had 3 meals
Math needs a subexpression: 6 half-meals
Tab:	|  Dollar: $5  Quote: "hi"
It's Ember's cave
Report for Ember
  meals: 3

The last part is a here-string: @" and "@ on lines of their own wrap a multi-line string. @' ... '@ is the literal version. They’re perfect for templates, help text and embedded CSV or JSON.

Split, join and replace

OperationExampleResult
-split (regex)'a, b,c' -split ',\s*'a, b, c
-split (unary, on whitespace)-split ' ember glim 'ember, glim
-join'a', 'b' -join '-'a-b
-replace (regex)'2026-10-02' -replace '(\d+)-(\d+)-(\d+)', '$3/$2/$1'02/10/2026
.Trim() .TrimEnd('.')' hi '.Trim()hi
.Substring(start, length)'Thornjaw'.Substring(0, 5)Thorn
.PadLeft(n, '0')'42'.PadLeft(4, '0')0042
.StartsWith() .Contains()'Glim'.StartsWith('G')True (case-sensitive!)

Use single quotes for the replacement in -replace, so $1 reaches the regex engine instead of being expanded as a (probably empty) PowerShell variable.

text-tools.ps1
1$line = '07:30 | Glim |  1 lantern moth '
2$time, $dragon, $food = $line -split '\s*\|\s*'
3"[$time] [$dragon] [$($food.Trim())]"
4'ember', 'glim', 'skyla' -join ', '
5'Ember the GRUMPY' -replace 'grumpy', 'misunderstood'
6'2026-10-02 Ember fed' -replace '^(\d{4})-(\d{2})-(\d{2})', '$3/$2/$1'
7'DRG-' + '42'.PadLeft(4, '0')
8$words = -split ' the   quick  wyrm '
9$words.Count
Output
[07:30] [Glim] [1 lantern moth]
ember, glim, skyla
Ember the misunderstood
02/10/2026 Ember fed
DRG-0042
3

Notice the first line: assigning to several variables at once takes the pieces of an array in order. And -replace ignored case when it found GRUMPY - use -creplace to be strict.

Key takeaways

  • Single quotes are literal; double quotes expand $var and $( ).

  • The backtick escapes in double quotes: `n, `t, `$.

  • Here-strings (@" "@, @' '@) hold multi-line text.

  • -split, -join and -replace use regex; keep -replace’s replacement in single quotes.

Lesson quiz

7 questions · pass with 5 correct · up to 50 XP

Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.

Practice: write PowerShell scripts

Write a script in the editor and run it for real against sample input, which is piped into your script as $input. Scripts run on PowerShell 6.2 through Try It Online (tio.run), a free public service, so these exercises avoid PowerShell 7-only syntax; your script and test input are sent there.

Exercise 1

Reformat the feeding times

+25 XP

Each input line looks like 2026-10-02T07:30 Ember 4: a date and time, a dragon and a number of meals. Rewrite each as Ember ate 4 meals at 07:30 on 02/10/2026 using -split or -replace.

  • Morning
  • New year
script.ps1
Loading editor…

Lessons teach PowerShell 7; your script runs on PowerShell 6.2 via Try It Online (tio.run), a free public service, so stick to syntax that works there. Test input is piped into your script as $input. Your script and test input are sent to that service.

Exercise 2

Hashtag maker

+25 XP

The sanctuary’s social media keeper needs hashtags. Each input line is a phrase with any amount of spacing. Turn it into # followed by every word with its first letter in capitals and the rest in lower case, then the word count in parentheses: ember the GRUMPY firedrake becomes #EmberTheGrumpyFiredrake (4 words).

  • Two phrases
  • Feeding time
script.ps1
Loading editor…

Lessons teach PowerShell 7; your script runs on PowerShell 6.2 via Try It Online (tio.run), a free public service, so stick to syntax that works there. Test input is piped into your script as $input. Your script and test input are sent to that service.

Questions about this lesson

Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.

Loading posts…

Did you like the lesson? 😆👍
Consider a donation to support our work: