Sign up for our newsletter.

I agree to this site's Privacy Policy.

Prof. Powershell

Match Play: PowerShell's Match Operator

Last time, we used the like operator to find wildcard type matches. Now, let's find exactly what we're searching for with -Match.

Last time we looked at using the Like operator to find wildcard type matches. The other operator you can use is -Match (and its opposite, -notmatch). This works very similar to -like except instead of using a wildcard, you use a regular expression pattern.

Now, before your eyes roll back in your head at the mention of regular expressions, let's look at an example of a pattern, which can be as simple as a string:

PS C:\> Get-Service | where {$_.status -match "running"}

In this context, we are still looking at a case-insensitive comparison. Unlike using like and wildcards, we can use -Match to find partial matches anywhere in a string:

PS C:\> get-process | where {$ -match "log"}

This will return all processes that have log anywhere in the name property. This is the same thing as running:

PS C:\> get-process | where {$ -like "*log*"}

For simple comparisons like this it probably doesn't matter which operator you use. Just remember that the match pattern "floats". However you can anchor it. For example, if we want to find all processes that start with "win" we can anchor our pattern with the ^ character:

PS C:\> get-process | where {$ -match "^win"}

This should return processes like winit and winlogon. Yes, I realize this not the best way to do this but it makes for a simple example. If we want to find something that is anchored at the end we use the $:

PS C:\> get-process | where {$ -match "host$"}

This should return processes like svchost, taskhost and conhost, depending on your operating system. You can also combine the two anchors:

PS C:\> get-process | where {$ -match "^powershell$"}

If you have the PowerShell ISE open as well as least one instance of the console, this example should only return the console process. If by chance you had something else running that had PowerShell in the name it too would not be returned. Granted, this is the same thing as running:

PS C:\> get-process | where {$ -eq "powershell"}

Again, my example is just an illustration of -match, not how to use Get-Process. But trust me, the -match operator will start getting more interesting once you learn a bit more about regular expressions, which we'll dive into next time.

About the Author

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog, on Twitter at and on Google Plus (http:/

comments powered by Disqus

Reader Comments:

Wed, Aug 8, 2012 EVVJSK

Nice articles so far. I have dabled in Powershell (previously done a decent amount of vbscript programming on small and large utility projects), but only a bit of Powershell. These short "hits" of Powershell scripting are something that can be digested in about 5 minutes. I may not remember everything there is to know, but have been exposed to the operators/keywords, I at least know more that I did before and can do further research as warranted. Plesae keep up these smaller articles and continue to show how to use them in quick examples ! Thanks.

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Please type the letters/numbers you see above