Merging contents of CSV's

Welcome Forums General PowerShell Q&A Merging contents of CSV's

Viewing 2 reply threads
  • Author
    Posts
    • #185411
      Participant
      Topics: 2
      Replies: 3
      Points: 13
      Rank: Member

      Hi

      I have seen many questions and answers from users who would like to combine the output of 2 csv's and remove any duplicates.

      How would we go about combining 2 csv's and ensure that ONLY the duplicates remain.

      So anything that is not on csv1 but is on csv2 needs to be removed for example.

      I have 2 csv's with machine names. One from Azure and one from AD. I need to combine these.

      Could someone assist with this?

       

    • #185426
      Participant
      Topics: 9
      Replies: 1277
      Points: 1,051
      Helping Hand
      Rank: Community Hero
      1. Combine the objects on computername, there are many examples on how combine or join objects.
      2. Group-Object on ComputerName Where .Count -gt 1
    • #185438
      Participant
      Topics: 0
      Replies: 6
      Points: 43
      Rank: Member

      I would try something like this. The key here is the -Passthru variable:

      
      Compare-Object -ReferenceObject $newCSV2 -DifferenceObject $newCSV1 -property IP -PassThru |
      Where-Object {$_.sideindicator -eq '=>'} |
      Sort-object -property {[version]$_.ip} |
      select ip,dnsname,netbiosname,lastscanned |
      Format-Table -AutoSize
      
      

       

      • #185453
        Participant
        Topics: 2
        Replies: 3
        Points: 13
        Rank: Member

        Hi

        Thanks I will look into this.

        I assume I would store both my csv's in a variable. As you have done above?  One variable named $newCSV2 and one named $newCSV1

        I tried the below this does not error but the resulting csv file is blank.

        Am I along the right lines here?

         

        $csv1 = Import-Csv 'C:\Users\birrelld\Desktop\Project Work\Stale machine on Azure\Exported CSV\AD\SDS.xlsx'
        $csv2 = Import-Csv 'C:\Users\birrelld\Desktop\Project Work\Stale machine on Azure\Exported CSV\Azure\sdsazure.xlsx'

        Compare-Object -ReferenceObject $csv1 -DifferenceObject $csv2 -property IP -PassThru |
        Where-Object {$_.sideindicator -eq '=>'} |
        Sort-object -property {[version]$_.ip} |
        select name | export-csv C:\Users\birrelld\Desktop\test2.csv
        Format-Table -AutoSize

         

Viewing 2 reply threads
  • You must be logged in to reply to this topic.