I have two CSV files with tab-separated values.
CSV1:
GivenName Surname DisplayName Manager
John Smith John Smith Oliver Twist
CSV2:
SAMAccountName mail DistinguishedName
John Smith johns@example.com CN=John Smith,CN=Users,DC=example,DC=com
Oliver Twist olivert@example.com CN=Oliver Twist,CN=Users,DC=example,DC=com
What I've wanted to do is to compare the CSV1 with CSV2 and match the manager name in Manager
column in CSV1 file with CSV2 DistinguishedName
column value (i.e. "CN=Oliver Twist"
) as in this example and export to a new CSV file like this:
SAMAccountName mail Manager
John Smith johns@example.com Oliver Twist
There might be hundreds of distinguished names in CSV2 file as mentioned and all I want is output from comparing and matching the DistinguishedName
column value "CN=whatever name "
with the Manager
column in CSV1.
Here is the script I tried:
$csv1 = Import-Csv -Path 'C:temp\AD.csv'
$csv2 = Import-Csv -Path 'C:\temp\AD_ExportDN.csv'
$csv3 = foreach ($record in $csv2) {
$matchingRecord = $csv1 | Where-Object { $_.manager -eq $record.manager }
# if your goal is to ONLY export records with a matching email address,
# uncomment the if ($matchingRecord) condition
#if ($matchingRecord) {
$record | Select-Object *, @{Name = 'manager'; Expression = {$matchingRecord.DistinguishedName}}
#}
}
$csv3 | Export-Csv -NoTypeInformation -Force -Path 'C:\temp\merged_for_AD2.csv'