The following code worked for me (Thank you, @Mathias R. Jessen !)
$content1 = Import-Csv -Path "C:\Users\blah\Desktop\Test\test1.csv"
$content2 = Import-Csv -Path "C:\Users\blah\Desktop\Test\test2.csv"
# create hash table to be used as a mapping table
$nameToIDsMap = @{}
# add each row from the csv to the mapping table - use realName as key, userName as value
$content1 | ForEach-Object {
$nameToIDsMap[$_.realName] += @($_.userName)
}
$content2 | ForEach-Object {
$personRecord = $_
$nameToIDsMap[$personRecord.Name] |ForEach-Object {
[pscustomobject]@{
UserName = $_
RealName = $personRecord.Name
Roles = $personRecord.ApprovedGroups
}
}
} | Export-Csv "C:\Users\blah\Desktop\Test\test3.csv" -NoTypeInformation