Merging contents of CSV's

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?

 

  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

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

[pre]

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

[/pre]

 

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