I have a Match[]
from string matching, each Match
has a Groups
property which is a GroupCollection
. I want to map the Match[]
to a GroupCollection[]
, and I'm trying to do this with a pipeline's output using ForEach-Object
.
The issue is that whenever I access and return Match[x].Groups
in the script block, the collection gets enumerated and outputted as its contents, essentially expanding and flattening the GroupCollection
's and losing any instance properties.
Is there a way to pull this off with an pipeline output? Maybe ForEach-Object
isn't the right function to use.
$String = "12345"
$Regex = "(?<number>\d)"
$MatchArr = ($String | Select-String -Pattern $Regex -AllMatches).Matches # Match[]
$DesiredOutput = $MatchArr[0].Groups, $MatchArr[1].Groups, $MatchArr[2].Groups # etc..
Get-Member -InputObject $DesiredOutput[0] # "GroupCollection"
$WrongOutput = $MatchArr | ForEach-Object {$_.Groups} # Resolves and outputs the collection contents, making a flat array of all the contents
Get-Member -InputObject $WrongOutput[0] # "Match" $WrongOutput[0] == $MatchArr[0].Groups[0], AKA the actual contents