How do I include this field into Out-gridview

# --- $groups is 6 separate .TXT files
$groups=@(Grp-1,Grp-2,Grp-3,Grp-4,Grp-5,Grp-6)

# --- $titles is a .CSV file, each line containing a few words (book title)
$titles = Import-csv .\Mysource.csv

# --- check existence of each line in $titles in each of the 6 groups in $groups
$dups = Foreach ($mt in $titles) {
            [string]$mtstring = $mt.TitleWoYear
            Foreach ($g in $groups) {
                 $Gfile = 'C:\TEMP\ + $g + '.txt'
                 $thisgroup = Get-content -path $Gfile
                 $thisgroup | Out-string -stream |
                 Select-string -pattern $mtstring -simplematch -list
            }
        }
$dups | Out-gridview

The above works OK. I have grappled with how I can include the value of $g (a file name) into the output using Out-Gridview (if possible?) I tried reversing the inner Foreach loop with the outer Foreach loop but discovered the -Begin / -Process / -End blocks are not allowed in Foreach ($a in $b) looping, only for Foreach-object (pipelining). Would be grateful for some advice or insights/tips. Many thanks.

Using Out-GridView in a script will effect automation, especially in loops. But If you really want to do this, you can put it inside the inner foreach as well. Else you can use Foreach-Object for $groups.

Foreach($mt in $titles){
...
$groups | ForEach-Object ...
...
}

Yes, Mr KVPrasoon … I’m glad you sensitized me to this aspect of the impact of Out-gridview during processing. Indeed, it can become disruptive. What I typically do is insert Out-gridview at “key points” when I am still developing the script, and then comment it out when “final”.

My sincerest thanks for your continuing advice and assistance, and generous sharing of technical knowledge.