Updated March 4, 2023
Definition of PowerShell Match
PowerShell match operators (Like, NotLike, Match, NotMatch) checks if the Input string or keyword matches the specific keyword using the provided pattern or the Wildcard. Patterns and the Wildcard used depends on the operator that is used. -Like and -NotLike operators use the wildcard characters and -Match and -NotMatch operators use the regular expressions.
Syntax:
Below syntax is used for the PowerShell Match Operators.
<string[]> -like <wildcard-expression>
<string[]> -notlike <wildcard-expression>
<string[]> -match <regular-expression>
<string[]> -notmatch <regular-expression>
How do Match Operators work in PowerShell?
A . -Like and -NotLike operators.
PowerShell -Like and -NotLike operators are similar to -Eq or -Ne operators but they use the wildcard character to match the input string. Below are the wildcard characters that are used for the matching pattern.
Wildcard characters:
* | Match Zero or More Characters |
? | Match one character in that position |
[] | Match a range of characters |
[] | Match specific characters |
Wildcard character (*) matches zero or more characters in the string and if the input string is secular then it gives the boolean output and if it is a string array then the matching strings will be the output as shown below.
"PowerShell" -like "Power*"
Output:
In the above example, wildcard character (*) matches the string before it (case-insensitive).
"PowerShell" -like "Power"
Output:
As shown above, -like operator doesn’t work without the wildcard character. When we use wildcard character before the string it checks if it matches the ending part of the string as shown below.
"PowerShell" -like "*Shell"
Output:
When a keyword is added between the two wildcard characters (*), it checks if the keyword is part of the input string. For example,
"PowerShell" -like "*rsh*"
Output:
-NotLike operator will give the contrary output.
"PowerShell" -Notlike "*rsh*"
Output:
The above examples are for a single input string. If there is more than one string as an input value then it returns the matching value from the input string.
"PowerShell","Azure","Insight" -like "*Sh*"
Output:
-NotLike example.
"PowerShell","Azure","Insight" -notlike "*Sh*"
Output:
Wildcard ([] – Square brackets) used to match the specific characters or the range of characters.
When the wildcard character [] is used with a dash (‘-‘), it matches the range of characters from the string. If found, returns the True value and otherwise return a false value.
"PowerShell" -like "p[m-p]wershell"
Output:
In the above example, the character ‘o’ in the range of [m-p] so the above output is true.
Similarly, if we use the character set inside the square bracket it matches the input character from them. For example,
"PowerShell" -like "[pq]owershell"
Output:
In the above example, the first character matches in the set [pq] so the output is true. The below output will be false because a character ‘p’ doesn’t belong to [abc] set.
"PowerShell" -like "[abc]owershell"
B. -Match and -NotMatch operators.
-Match and -NotMatch operators work with regular expressions.
- Character Literals:
When we use part of the characters to match, if the characters exist, -Match operator returns the True output, otherwise False. For example,
"IndPC002" -match "IND"
Output:
- Character Groups
"IndPC002" -match "[ijk]nd[opq]C002"
Output:
In the above example, character groups are compared with the input character at the particular position. If it matches, the output is returned, otherwise false.
- Character Ranges
"PS" -match "[A-Z][A-Z]"
Output:
"PS" -cmatch "[A-Z][0-9]"
Output:
- Decimal and Non-Decimal Digits.
RegEx character \d matches the decimal digits while \D matches any non-decimal digits.
2021 -match "\d"
Output:
"2021c" -match "\D"
Output:
The above output is true because it contains the non-digit character.
- Word characters.
To check if the input string contains the words, we need to use \w, and in contrast, \W indicates the non-word character class.
"PowerShell" -match "\w"
Output:
- Whitespace and Wildcards characters.
Whitespace is matched with ‘\s’ character while Non-WhiteSpace is matched with the \S character.
"a c" -match "[ab]\s[a-z]"
Output:
For wildcard character match period (.) symbol is used. It matches any character except newline (\n).
"a c" -match "..."
"a c" -match "...."
Output:
- Quantifiers
+ | One or More times |
* | Zero or More times |
? | Zero or one Time |
{n,m} | Min N, Maximum M |
Examples:
- Asterisk(*) matches the previous characters for zero or more times, even if the characters don’t exist, the output will be true.
"Hello PowerShell" -match "\w*\s*PowerShell"
Output:
- Plus (+) sign matches the previous character one or more times.
"IndNZPC-001" -match "[A-Z]+-\d+"
Output:
- Question Mark (?) matches the previous character zero or one time. In the below example, even if the dash (-) doesn’t exist, the output will be true.
"IndNZPC001" -match "[A-Z]+-?\d+"
Output:
- {n,m} qualifiers can be used in different ways as shown below in the table.
{n} | Match Exactly N number of time |
{n,} | Match atleast N number of times |
{n,m} | Min N and Maximum M times |
Example:
'1800-1800-123' -match '\d{4}-\d{4}-\d{3}'
Output:
- Using Anchors:
PowerShell uses mainly two anchors. Caret (^) and Dollar($).
Caret (^) matches the start of the string. For example, the below command will display all the processes starting with ‘d’.
Get-Process | where{$_.Name -match "^d"}
Output:
Dollar ($) is used at the end of the string to match. For example, the below command will retrieve all the processes ending with Host.
Get-Process | where{$_.Name -match "host$"}
Output:
You can also combine both caret (^) and Dollar ($) signs.
Examples
Example #1: Like operator to filter specific service.
Get-Service | where{$_.Name -like "sp*"}
Output:
The above example will retrieve all the service names starting with SP.
Get-Process | where{$_.Name -notlike "*win*"}
The above example will retrieve all the processes that don’t include “win” in the process name.
Example #2: Match operator for name validation
"IndPC-002" -match "\w-\d"
"IndPC002" -match "\w-\d"
Output:
The above example validates the Server name pattern.
Example #3: PowerShell function ValidatePattern
We can use the same above example with the Function ValidatePattern attribute as shown below.
function ServerValidation{
param(
[ValidatePattern("\w-\d")]$servername
)
Write-Output "Server Name is valid"
}
Output:
Conclusion
PowerShell Match operators are very useful when we are dealing with the Strings in PowerShell and besides, to validate input form we can use the Match parameter so that we don’t need to write the lengthy codes. These operators reduce the complexity of the script.
Recommended Articles
This is a guide to PowerShell Match. Here we discuss the definition, syntax, parameters, and How do Match Operators work in PowerShell? with code implementation. You may also have a look at the following articles to learn more –