It looks like you're splitting and replacing it to get the file content into individual lines. However, Get-Content does that by default. Moreover, in your code $line & $content would have the same values.
I'm giving this a shot but I think what you are trying to do is look forward through an array of lines to find the group name that may come at some arbitrary point after you spot the "approver".
I manually typed out a file as a pretty good match to what you demonstrated in the revised question. Came up with the below. Again this is off the top of my head, but it seems to work as you dictated above.
Note: I added the $Groups array just to simulate what you said in your last comment.
$File = 'C:\Temp\InputTest.txt'
$Groups = @( 'Group1','Group2','Group3','Group4','Group5' )
$Lines = Get-Content $File
$Output =
For( $i = 0; $i -lt $Lines.Length; ++$i )
{
If( $Lines[$i] -match '^# approver' )
{
$Approver = $Lines[$i].split(':').Trim()[1]
# Need to ignore lines in between
:LookForGroup Do
{
++$i
ForEach ( $Group in $Groups )
{
If( $Lines[$i] -match "^$Group" )
{
$Approver + ' ' + $Lines[$i]
++$i
}
ElseIf( $Lines[$i] -match "(^$|\r\n|^######)")
{# Looking for a signal to move on...
Remove-Variable Approver #Maybe don't need this???
Break LookForGroup
}
}
} While($true)
}
}
#This will display the output, you can do something else with it...
$Output
What I'm doing is using $i kind of like a cursor. Once an approver line is spotted I go through each group to see if the line matches the group name.If yes I output the approver and the group name as you specified. I then increment $i thus moving one line forward in the file. If the line doesn't match the group I check for some characteristics to see if we've reached the end of a section (as defined by your example). If yes I'll break the Do loop which will cause the For loop to continue, note: that $i will remain as we set it...
I admit this is a little kludge let me know...