0
$data1 = Import-Csv "SASSO1.csv" | ?{$_.SourceType -eq "file"} | select "FullPath"
$data2 = Import-Csv "SASSO2.csv" | ?{$_.SourceType -eq "file"} | select "FullPath"

$data1 | Where-Object -FilterScript { $_.FullPath -notin $data2 } | Export-Csv "RISULTATO.csv" -NoTypeInformation

My question is, how to find the difference between the two csv files, where it takes only one column or FullPath?

Filburt
  • 17,626
  • 12
  • 64
  • 115
Sunflower
  • 3
  • 1
  • 1
    Does this answer your question? [Using Perl or Powershell, how to compare 2 CSV files and get only the new rows?](https://stackoverflow.com/questions/50590776/using-perl-or-powershell-how-to-compare-2-csv-files-and-get-only-the-new-rows) – Filburt Jun 22 '22 at 13:13
  • unless I'm misunderstanding something, you should be able to use `Compare-Object` in this case. – Abraham Zinala Jun 22 '22 at 13:53
  • Using this [`Join-Object script`](https://www.powershellgallery.com/packages/Join)/[`Join-Object Module`](https://www.powershellgallery.com/packages/JoinModule) (see also: [In Powershell, what's the best way to join two tables into one?](https://stackoverflow.com/a/45483110/1701026)): `Import-Csv .\SASSO1.csv |Outer-Join (Import-Csv .\SASSO2.csv) -on FullPath` – iRon Jun 22 '22 at 14:57

1 Answers1

0

You can use Compare-Object to make such comparisons.

Example

$data1 = @'
FullPath,Name,Desc
a,a,a
b,b,b
c,c,c
'@ | ConvertFrom-Csv

$data2 = @'
FullPath,Name,Desc
d,d,d,
e,e,e
c,c,c
'@ | ConvertFrom-Csv


Compare-Object -ReferenceObject $data1 -DifferenceObject $data2 -Property FullPath 

Results

FullPath SideIndicator
-------- -------------
d        =>
e        =>
a        <=
b        <=

By looking at the results, you can see that the record d and e are in $data2 only and a and b on $data1 only.

Based on that, you can export these record or do whatever you want with them. You can use the -PassThru switch if you want to get the complete objects along with the SideIndicator property so you get something like the following results

FullPath Name Desc SideIndicator
-------- ---- ---- -------------
d        d    d    =>
e        e    e    =>
a        a    a    <=
b        b    b    <=

That way, you can simply select the properties to export | Select-Object -Property FullPath,Name,Desc and export all the results that are not in both database.

You can also use the -IncludeEqual switch and possibly the -ExcludeDifferent switch if instead you want the results present in both csv to be included.

Finally, if you want just one side of the comparison (eg: only the records missing from $data1), then you can filter the results using a simple where statement (eg: | Where-Object -Property Sideindicator -eq '=>').

Sage Pourpre
  • 9,932
  • 3
  • 27
  • 39