Strings, Formatting and Regex

By the end of this lesson you'll format text precisely with the -f operator and here-strings, slice and stitch strings with methods, -split, and -join, and wield regular expressions through -replace, -match/$Matches, and Select-String — the toolkit for parsing logs, reshaping data, and validating input.

Learn Strings, Formatting and Regex in our free PowerShell course — an interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Powershell course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Working with text is like being a tailor . The -f operator is your tape measure and pattern — it lays values into exact, aligned slots. -split and -join are scissors and needle : cut a string into pieces, then stitch them back the way you want. And regex is your master pattern — a reusable template that recognises a shape (a date, an email, a log level) anywhere it appears, so you can find it, extract it, or reshape it with surgical precision.

1. Formatting with -f & Here-Strings

The -f operator fills numbered placeholders ( {' '} , {' '} , …) with values and supports .NET format specifiers after a colon — {' '} for two decimals, {' '} for a percentage, {' '} for alignment. A here-string ( ) holds multiple lines verbatim and still expands $variables , which is perfect for templates and reports. Read this worked example, then run it.

2. String Methods, -split & -join

Because strings are .NET objects, they carry methods like .Trim() , .ToUpper() , .Replace() , and .Substring() . To break a string apart, use the -split operator (its delimiter is a regex); to glue an array back together, use -join . These two are the bread and butter of turning raw text into structured data and back.

Now you try. Fill in the two blanks using the hints in the comments, then run it.

3. Regular Expressions: -match , $Matches & -replace

The -match operator tests a string against a regex and, on success, fills the automatic $Matches hashtable: $Matches[0] is the whole match, numbered/named groups follow. -notmatch is its inverse. The -replace operator edits text using regex and back-references ( $1 , $2 ) in the replacement. Remember these operators are case-insensitive by default — prefix with c ( -cmatch ) for case sensitivity.

PowerShell's escape character is the backtick , not the backslash. Inside double quotes, n is a newline. But in a regex pattern, the backslash still has its normal regex meaning ( \d , \. ) — so write regex patterns in single quotes to stop PowerShell touching the backslashes.

Select-String is PowerShell's grep : it searches piped text or files on disk for a regex and returns rich match objects (with file name and line number). Add -SimpleMatch for a literal, non-regex search.

Each result is a MatchInfo object, so you can pipe it onward — e.g. Select-String 'ERROR' app.log | Select-Object LineNumber, Line .

No blanks this time — just a brief and an outline to keep you on track. Build it, run it, and check your output against the example in the comments. Pulling structured fields out of raw log text is one of the most common real-world scripting jobs.

Practice quiz

What does the -f format operator do?

  • Inserts values into a format string by index
  • Forces a type
  • Finds a file
  • Filters a collection

Answer: Inserts values into a format string by index. The -f operator substitutes values into placeholders like {0} and {1} in a .NET format string.

Which syntax creates a here-string in PowerShell?

  • @"..."@
  • <<EOF ... EOF

Answer: @"..."@. A here-string is bounded by @" on its own line and "@ on its own line, preserving multiple lines verbatim.

Which operator splits a string into an array?

  • -cut
  • -divide
  • -split
  • -join

Answer: -split. -split breaks a string into an array on a delimiter (which is treated as a regex pattern).

Which operator joins an array into a single string?

  • -merge
  • -join
  • -split
  • -concat

Answer: -join. -join combines array elements into one string, optionally with a separator between them.

What does the -replace operator use for its pattern?

  • Glob patterns
  • Plain text only
  • Wildcards
  • A regular expression

Answer: A regular expression. -replace treats its first argument as a regex pattern and supports replacement back-references.

After a successful -match, where are captured groups stored?

  • $Matches
  • $Groups
  • $Captures
  • $Result

Answer: $Matches. A successful -match populates the automatic $Matches hashtable with the whole match and any captured groups.

What does -notmatch return?

  • The match count
  • True when the regex does NOT match
  • The first match
  • An array of matches

Answer: True when the regex does NOT match. -notmatch is the inverse of -match: it returns True when the pattern does not match the string.

Which cmdlet searches text or files for a regex, like grep?

  • Search-File
  • Find-String
  • Match-Text
  • Select-String

Answer: Select-String. Select-String searches input or files for a pattern and returns match objects with line numbers.

In a double-quoted string, how do you write a literal dollar sign?

  • It is not possible
  • Double it: $
  • Escape it with a leading backtick character
  • Use a leading backslash

Answer: Escape it with a leading backtick character. PowerShell's escape character is the backtick, which placed before a dollar sign produces a literal dollar sign and avoids variable expansion.

Which comparison operator does a case-INSENSITIVE regex match by default?

  • -cmatch
  • -match
  • -clike
  • -exact

Answer: -match. By default -match is case-insensitive; prefix with c (-cmatch) to make it case-sensitive.