EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Match
 

PowerShell Match

Chirag Nagarekar
Article byChirag Nagarekar
EDUCBA
Reviewed byRavi Rathore

Updated March 4, 2023

PowerShell Match

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

powershell match 1

In the above example, wildcard character (*) matches the string before it (case-insensitive).

"PowerShell" -like "Power"

Output:

powershell match 2

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:

powershell match 3

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:

powershell match 4

-NotLike operator will give the contrary output.

"PowerShell" -Notlike "*rsh*"

Output:

powershell match 5

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:

powershell match 6

-NotLike example.

"PowerShell","Azure","Insight" -notlike "*Sh*"

Output:

notlike

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:

like plm

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:

pqlowershell

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"

abc lowershell

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:

match

  • Character Groups

"IndPC002" -match "[ijk]nd[opq]C002"

Output:

Indopq

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:

powershell match 7

"PS" -cmatch "[A-Z][0-9]"

Output:

powershell match 8

  • Decimal and Non-Decimal Digits.

RegEx character \d matches the decimal digits while \D matches any non-decimal digits.

2021 -match "\d"

Output:

decimal

"2021c" -match "\D"

Output:

match 1

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:

word character

  • 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:

wild

For wildcard character match period (.) symbol is used. It matches any character except newline (\n).

"a c" -match "..."
"a c" -match "...."

Output:

-match

abc

  • 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:

astr 1

  • Plus (+) sign matches the previous character one or more times.

"IndNZPC-001" -match "[A-Z]+-\d+"

Output:

plus

  • 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:

question mark

  • {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:

powershell match 9

  • 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:

anchors

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:

dollars

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:

example 1

The above example will retrieve all the service names starting with SP.

Get-Process | where{$_.Name -notlike "*win*"}

notlike

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:

match 2

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:

example 3

 

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 –

  1. PowerShell Invoke-Webrequest
  2. PowerShell Wait
  3. PowerShell Sleep
  4. PowerShell SubString

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW