PowerShell -match Operator: Regex Examples

The PowerShell -match operator checks a string against a regular expression and returns True or False. When it matches, the matched text is saved in the automatic $Matches variable:

$note = 'Order ORD-5001 shipped to Austin on 2026-10-02'
$note -match 'ORD-\d{4}'
$Matches[0]

Output:

True
ORD-5001

\d{4} means four digits, so the pattern finds the order number anywhere in the text. Below I cover case sensitivity, named groups, arrays, -notmatch, special characters and finding every match.

-match is case-insensitive

Like most PowerShell operators, -match ignores case. Use -cmatch when case matters:

'Shipped to AUSTIN' -match 'austin'
'Shipped to AUSTIN' -cmatch 'austin'

Output:

True
False

There’s also -imatch, which is the same as -match but makes the intent explicit. All of them are described in about_Comparison_Operators.

For simple wildcards without regex, the -like operator is easier.

Pull out parts with named groups

Parentheses capture parts of the match. Give them names with (?…), and read them from $Matches by name:

$server = 'NYC-SQL-01'
if ($server -match '^(?<City>[A-Z]{3})-(?<Role>[A-Z]+)-(?<Number>\d+)$') {
    "City:   $($Matches.City)"
    "Role:   $($Matches.Role)"
    "Number: $($Matches.Number)"
}

Output:

City:   NYC
Role:   SQL
Number: 01
PowerShell -match named capture groups
Splitting a server name into city, role and number (PowerShell 7)

The ^ and $ anchors make the whole name match, not just part of it. Always use $Matches inside the if block, because it keeps its old value when a match fails.

Use -match on an array

With an array on the left, -match works as a filter and returns the items that match:

$servers = 'NYC-SQL-01', 'DAL-WEB-02', 'NYC-WEB-03', 'SEA-SQL-04'
$servers -match '-SQL-'

Output:

NYC-SQL-01
SEA-SQL-04

In this case it doesn’t return True or False, and it doesn’t set $Matches. For the same filter on objects, use Where-Object, as in Where-Object with multiple conditions.

Find values that don’t match with -notmatch

-notmatch is the opposite. Here it finds phone numbers that aren’t in the 512-555-0147 format:

$phones = '512-555-0147', '555-0199', '(214) 555-0123', '737-555-0182'
$phones -notmatch '^\d{3}-\d{3}-\d{4}$'

Output:

555-0199
(214) 555-0123

This is a quick way to spot bad rows before you import a CSV file. To check a pattern inside files instead, use Select-String.

Escape special characters

Characters such as $, ., ( and + mean something in a regular expression. A literal dollar amount won’t match until you escape it:

$line = 'Total due: $249.99'
$line -match '$249.99'
$line -match [regex]::Escape('$249.99')
[regex]::Escape('$249.99')

Output:

False
True
\$249\.99
PowerShell -match with regex Escape
The unescaped pattern fails, the escaped one matches (Windows PowerShell 5.1)

[regex]::Escape() adds the backslashes for you. If you just need a plain text check with wildcards, -like is simpler. See the PowerShell -like operator.

Get every match, not just the first

-match stops at the first match. [regex]::Matches() returns all of them:

$invoice = 'Monitor $249.99, Keyboard $89.50, Dock $310.00'
$invoice -match '\$\d+\.\d{2}' | Out-Null
"With -match:  $($Matches[0])"
"All matches:  $([regex]::Matches($invoice, '\$\d+\.\d{2}').Value -join ', ')"

Output:

With -match:  $249.99
All matches:  $249.99, $89.50, $310.00