I'm trying to extract text from a set of files on Windows using the Powershell (version 4):

PS > Select-String -AllMatches -Pattern <mypattern-with(capture)> -Path file.jsp | Format-Table

So far, so good. That gives a nice set of MatchInfo objects:

IgnoreCase                    LineNumber Line                          Filename                      Pattern                       Matches
----------                    ---------- ----                          --------                      -------                       -------
	True                            30   ...                           file.jsp                      ...                           {...}

Next, I see that the captures are in the matches member, so I take them out:

PS > Select-String -AllMatches -Pattern <mypattern-with(capture)> -Path file.jsp | ForEach-Object -MemberName Matches | Format-Table

Which gives:

Groups        Success Captures                 Index     Length Value
------        ------- --------                 -----     ------ -----
{...}         True    {...}                    49        47     ...

or as list with | Format-List:

Groups   : {matched text, captured group}
Success  : True
Captures : {matched text}
Index    : 39
Length   : 33
Value    : matched text

Here's where I stop, I have no idea how to go further and obtain a list of captured group elements.

I've tried adding another | ForEach-Object -MemberName Groups, but it seems to return the same as the above.

The closest I get is with | Select-Object -Property Groups, which indeed gives me what I'd expect (a list of sets):

Groups
------
{matched text, captured group}
{matched text, captured group}
...

But then I'm unable to extract the captured group from each of them, I tried with | Select-Object -Index 1 I get only one of those sets.


Update: a possible solution

It seems that by adding | ForEach-Object { $_.Groups.Groups[1].Value } I got what I was looking for, but I don't understand why - so I can't be sure I would be able to get the right result when extending this method to whole sets of files.

Why is it working?

As a side note, this | ForEach-Object { $_.Groups[1].Value } (i.e. without the second .Groups) gives the same result.

I'd like to add that, upon further attempts, it seems the command can be shortened by removing the piped | Select-Object -Property Groups.

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: