I\'m trying to extract text from a set of files on Windows using the Powershell (version 4):
PS > Select-String -AllMatches -Pattern
According to the powershell docs on Regular Expressions > Groups, Captures, and Substitutions:
When using the -match operator, powershell will create an automatic variable named $Matches
PS> "The last logged on user was CONTOSO\jsmith" -match "(.+was )(.+)"
The value returned from this expression is just true|false, but PS will add the $Matches hashtable
So if you output $Matches, you'll get all capture groups:
PS> $Matches
Name Value
---- -----
2 CONTOSO\jsmith
1 The last logged on user was
0 The last logged on user was CONTOSO\jsmith
And you can access each capture group individually with dot notation like this:
PS> "The last logged on user was CONTOSO\jsmith" -match "(.+was )(.+)"
PS> $Matches.2
CONTOSO\jsmith
Additional Resources: