PowerShell How-To

Matching and Using Regex Groups with PowerShell

Even though it has somewhat of a checkered reputation, being able to wield it will save you a ton of time.

There's a saying that I've heard numerous times about regular expressions (regex). It goes something like this: "If you have a problem that requires regex to fix then you've got two problems." Needless to say, regex is known for being hard to work with. This might be the case if you need to match lots difficult strings but typically I've found that I don't need to get complicated. Even though regex has a checkered past, don't let this sway you from using it when you need to.

When you need to leverage regex in PowerShell you'll be using the .NET regex engine. Each language has its own subtleties so be aware of this if you find an example regular expression on the Internet somewhere and it doesn't work how you'd expect. The regex pattern might be similar which may be hard to spot but just one character makes a world of difference when using regex.

So what if you're writing a PowerShell script and you'd like to use regex to match strings? Two options that you have are:

  1. Use the [regex] type accelerator.
  2. Use the automatic variable $matches.

Let's go over each one and some examples on their usage. First, we'll cover the [regex] type accelerator. To use the [regex] type accelerator requires using [regex] and proceeding with double colons.


Once you type this, start hitting Tab. By doing so, you will expose all of the properties and methods that you can use the the [regex] type accelerator. For our purposes we're going to be focusing on the Match() method. The Match() method is a way to instruct PowerShell to attempt to match a string inside of another string. The Match() method has two parameters; the string you'd like to match on and the regular expression you'd like to test against.

Let's say you have a string abc123 and want to check to see if that string starts with an a. To do that, you can use the [regex] type accelerator. You can see that the string abc123 does start with an a because of the Success property is True.

Figure 1.

Now what if you'd like to know what the string that was matched? To do this, we'll use regex groups. Whenever a match is found and a regex group is used; (), the [regex] type accelerator has a Captures property. This Captures property then has a property called Groups. This is a collection that contains lots of attributes of what was matched. The second element in that collection contains the actual value that was matched.

Figure 2.

You'll see that I was able to see that the string bc123 was the value that was matched inside the group.

Now let's do the same task only this time using the automatic variable $matches. When using the -match and -notmatch operators, if a match is found, PowerShell will populate a variable called $matches automatically. $Matches is a collection that contains all of the match objects that were last found.

Coming from our last example, let's try to do the same thing using the $matches variable.

Figure 3.

You'll see that this method is a little easier. I don't have to go through lots of different objects to find the desired value.

We just barely scraped the surface of what you can do with regular expressions in PowerShell. If you'd like more information on this topic I suggest visiting a blog post by John Cook. It goes into much more detail and covers a wide array of topics on using regular expressions in PowerShell.

About the Author

Adam Bertram is an independent consultant, technical writer, trainer and presenter. Adam specializes in consulting and evangelizing all things IT automation mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 powershell.org PowerShell hero and has numerous Microsoft IT pro certifications. He is a writer, trainer and presenter and authors IT pro course content for Pluralsight. He is also a regular contributor to numerous print and online publications and presents at various user groups and conferences. You can find Adam at adamtheautomator.com or on Twitter at @adbertram.

comments powered by Disqus

SharePoint Watch

Sign up for our newsletter.

I agree to this site's Privacy Policy.