Prof. Powershell

Regular Expressions, Part 3

A last look at expressions, this time by trying to match on opposites.

Sometimes we want to match on opposites. For example, use a regular expression to find everything that is NOT a word character. We know what will happen with this:

PS C:\> 'Power$h3|| is fun' -match "\w+"
PS C:\> $matches

Name      Value
----      -----
0         Power

The opposite is the same pattern but a capital W. Remember, I told you these patterns can be case sensitive:

PS C:\> 'Power$h3|| is fun' -match "\W+"
PS C:\> $matches

Name      Value
----      -----
0         ||

Those spaces are also "matchable". The class \s will match any whitespace:

PS C:\> 'Power$h3|| is fun' -match "\s"

The actual match value isn't much help because it is a space. But you might use this pattern as a validator:

$p=Read-Host "Enter a password"
If ($p -match "\s") {
   Write-warning "Your password has a space which I don't like"

What do you think we use to match on all non-white spaces?

PS C:\> 'Power$h3|| is fun' -match "\S+"
PS C:\> $matches

Name      Value
----      -----
0         Power$h3||

The regular expression matches up until it doesn't,which in this case is the first space. But notice it grabbed numbers and the special characters. This is a handy class when you want to match everything BUT spaces or tabs, but it is very broad.

The last "opposite" is with digits. Normally use use \d to match a number. Use \D to match non-numbers.

PS C:\> 'Power$h3|| is fun' -match "\D+"
PS C:\> $matches

Name      Value
----      -----
0         Power$h

The regular expression matched until it hit a number.

So let's see everything we've covered thus far is some more practical examples. Open up a PowerShell prompt and try these commands. Before you run the command, predict what type of result you will get. You might be surprised where you can use regular expressions.

get-service | where {$ -match "\d{2}"}
get-service [a-b]*
get-process [cw]*
dir c:\windows | where {$_.PSIsContainer -AND $ -match "\d$"}
dir c:\windows | where {$ -match "\s"}
dir HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\kb[0-9]*

I can't guarantee you'll get something for every single command, especially the registry example. But I hope you take the time to try them out. Regular expressions are extremely valuable in PowerShell and like any language element, practice makes perfect.

If you haven't already done so, be sure to read the help topic About_Regular_Expressions.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.