Regular Expressions in PowerShell

We’ll be using this $text variable throughout this short RegEx demo:



The -Match operator will return after the first match. This returns $True if a successful match was found and $False if not. If capturing groups are used in the expression the results are stores in the special $Matches hashtable. Item 0 contains the entire line that triggered the match and each additional item contains the matching text from the capture group.



Select-String -AllMatches

The Select-String has an -AllMatches option that will return all matches (duh!). This returns a [Microsoft.PowerShell.Commands.MatchInfo] object. Match info is stored in the .Matches property, which is of type [System.Text.RegularExpressions.Match]. The .Matches.Value property contains the entire line that matched. The .Matches.Groups.Value property contains text within a capture group