PowerShell Regular Expressions
A regular expression (regex) is a pattern that describes text: "four digits, a dash, two digits", "a word followed by @ and a domain". PowerShell uses the .NET regex engine everywhere: in -match, -replace, -split, switch -Regex, Select-String and the [regex] class.
Pattern basics
| Pattern | Matches |
|---|---|
. |
any character except newline |
\d \w \s |
a digit, a word character (letter/digit/_), whitespace |
\D \W \S |
the opposites |
[abc], [a-z], [^0-9] |
one character from a set / range / not in it |
* + ? |
0 or more, 1 or more, 0 or 1 |
{3}, {2,4} |
exactly 3, between 2 and 4 |
^ $ |
start, end of the text (or line with (?m)) |
\b |
word boundary |
( ), (?<name> ) |
capture group, named group |
a|b |
a or b |
\. \$ \\ |
a literal dot, dollar, backslash |
-match and $Matches
-match returns True or False for a single string and fills the $Matches hashtable with the first match:
powershell · try it yourself
$entry = 'Order 4711 shipped to Oslo on 2026-03-14'
if ($entry -match '(\d{4})-(\d{2})-(\d{2})') {
"Whole match: $($Matches[0])"
"Year $($Matches[1]), month $($Matches[2]), day $($Matches[3])"
}
if ($entry -match 'Order (?<id>\d+) shipped to (?<city>\w+)') {
"Order $($Matches.id) -> $($Matches.city)"
}
'abc' -match '^\d+