How to combine CSV's into one file based on mailaddress

André Borgeld 431 Reputation points
2020-11-27T08:14:36.483+00:00

I have two CSV's with information from Active Directory and from an Oracle Database, I would like to add the info from one CSV to the other and combine the information based the mailaddress (that's the link to combine the info).

I can import one file with import-csv. I can loop through the records with a for each.
But how do I link the right info from one CSV to the other based on the mailaddress of a user

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,552 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue 37,706 Reputation points Microsoft Vendor
    2020-11-27T10:15:24.417+00:00

    Hi,

    Hop this works for you.

    #Combin 1.csv and 2.csv into 3.csv  
    $FilePath1 = "D:\1.csv"  
    $FilePath2 = "D:\2.csv"  
    $FilePath3 = "D:\3.csv"  
    $users = Import-Csv -Path $FilePath1  
    $properties = ($users|Get-Member -MemberType NoteProperty)  
    $newuser=@()  
    Import-Csv -Path $FilePath2|ForEach-Object{  
        foreach($user in $users){  
            if($_.mailaddress -eq $user.mailaddress){  
                foreach($property in $properties){  
                    if($property.Name -ne "mailaddress"){  
                        $_ | Add-Member -NotePropertyName $property.Name -NotePropertyValue $user.($property.Name)}  
                }            
            }  
        }  
        $newuser +=$_  
     }  
     $newuser|Export-Csv -Path $FilePath3 -NoTypeInformation  
    

    Best Regards,
    Ian

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. André Borgeld 431 Reputation points
    2020-11-27T13:35:15.277+00:00

    Hey @Ian Xue

    Thank you very much for the idea, this is exactly what I'm looking for.

    Kind regards,

    Andre

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.