Introduction
The match operator helps you find if a text contains a certain pattern. It makes searching easy and fast.
$string -match $pattern
'Hello World' -match 'World'
'apple' -match '^a'
'123abc' -match '\d+'
$text = 'PowerShell is fun' if ($text -match 'fun') { Write-Output 'Pattern found!' } else { Write-Output 'Pattern not found.' }
-match operator do?-match-match operator is used to check if a string contains a pattern using regular expressions.-match.-match checks pattern presence [OK]-match finds patterns, not changes text [OK]-match with -replace$text contains the word "hello" using the match operator?-match placed between the variable and the pattern.$text -match "hello", which is the correct syntax. $text -matches "hello" uses -matches which is invalid.-match between variable and pattern [OK]-match exactly, no plural or missing dash [OK]-matches instead of -match$text = "PowerShell is fun"
if ($text -match "fun") { "Yes" } else { "No" }$text -match "fun" returns True.-match returns True [OK]$string = "Hello World"
if ($string -match) { "Match found" } else { "No match" }-match usage-match operator requires a pattern to match against, but none is provided here.-match needs a pattern [OK]-match [OK]-match. Which pattern correctly matches either word?| means OR, so "cat|dog" matches either "cat" or "dog".| for OR in regex patterns [OK]| for OR in regex patterns [OK]|& which means AND in some contextsPass the test and this pattern moves unknown