# Compare 2 csv files

**URL:** <https://forums.powershell.org/t/compare-2-csv-files/17881>\
**Category:** PowerShell Help\
**Created:** [November 5, 2021, 6:51pm UTC](https://forums.powershell.org/t/compare-2-csv-files/17881 "2021-11-05T18:51:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![matthew](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@matthew](https://forums.powershell.org/u/matthew)\
**Post date:** [November 5, 2021, 6:51pm UTC](https://forums.powershell.org/t/compare-2-csv-files/17881/1 "2021-11-05T18:51:25Z")

</div>

I have tried many approaches to this and everyone so far seems to be line by line based rather than data based. This is fine if there was only a new add because 1 file will have 1 extra line. If I add that new line to the middle of the file it will consider it and everything after it new.

What I am trying to do is take a csv file that is generated by a separate program and compare it to the previous days file. I am looking for any new data, modified data or deleted data only. I do have a unique column in the csv named User ID if that helps. Here are a few examples of what I am looking for:

This would generate an empty csv3

> csv1:
> 
> > User ID,Name,Phone  
> > 1,John,1234567890  
> > 2,Jane,0987654321  
> > 50,Mary,1111111111  
> > 55,Mark,2222222222
> 
> csv2:
> 
> > User ID,Name,Phone  
> > 1,John,1234567890  
> > 2,Jane,0987654321  
> > 50,Mary,1111111111  
> > 55,Mark,2222222222
> 
> csv3:
> 
> > User ID,Name,Phone

This would generate an add

> csv1:
> 
> > User ID,Name,Phone  
> > 1,John,1234567890  
> > 2,Jane,0987654321  
> > 50,Mary,1111111111  
> > 55,Mark,2222222222
> 
> csv2:
> 
> > User ID,Name,Phone  
> > 1,John,1234567890  
> > 2,Jane,0987654321  
> > 50,Mary,1111111111
> 
> csv3:
> 
> > User ID,Name,Phone,Action(new column)  
> > 55,Mark,2222222222,add

This would generate a modification

> csv1:
> 
> > User ID,Name,Phone  
> > 1,John,1234567890  
> > 2,Jane,0987654321  
> > 50,Mary,1111111111  
> > 55,Mark,2222222222
> 
> csv2:
> 
> > User ID,Name,Phone  
> > 1,John,1234567890  
> > 2,Max,0987654321  
> > 50,Mary,1111111111
> 
> csv3:
> 
> > User ID,Name,Phone,Action(new column)  
> > 2,Max,0987654321,old  
> > 2,Jane,0987654321,new

This would generate a delete

> csv1:
> 
> > User ID,Name,Phone  
> > 1,John,1234567890  
> > 2,Jane,0987654321  
> > 50,Mary,1111111111  
> > 55,Mark,2222222222
> 
> csv2:
> 
> > User ID,Name,Phone  
> > 1,John,1234567890  
> > 2,Max,0987654321  
> > 3,Jim,3333333333  
> > 50,Mary,1111111111
> 
> csv3:
> 
> > User ID,Name,Phone,Action(new column)  
> > 3,Jim,3333333333,delete

Here is the script that gets me the closest it will allow me to compare but only if the new line is at the end of csv.

> $path = “c:\ \*.csv”
> 
> $files = Get-ChildItem -Path $path |  
> Sort-Object LastWriteTime -Descending
> 
> $file1a = $files | Select-Object -First 1  
> $file2a = $files | Select-Object -First 1 -Skip 1
> 
> $file1 = Import-CSV -Path $file1a  
> $file2 = Import-CSV -Path $file2a
> 
> Compare-Object $file2 $file1 -PassThru | Export-CSV “c:\compare.csv” -NoTypeInformation

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 5, 2021, 8:05pm UTC](https://forums.powershell.org/t/compare-2-csv-files/17881/2 "2021-11-05T20:05:23Z")

</div>

Actually I’m pretty confused by your data samples. Could you please show the two input tables you want to compare and the result you expoect after the comparison?

I’d expect something similar to the following may work …

```auto
$CSV01 = Import-Csv -Path .\file01.csv -Delimiter ','
$CSV02 = Import-Csv -Path .\file02.csv -Delimiter ','

Compare-Object -ReferenceObject $CSV01 -DifferenceObject $CSV02 -PassThru 

```

---

<div class="post-metadata">

**Author:** ![matthew](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@matthew](https://forums.powershell.org/u/matthew)\
**Post date:** [November 5, 2021, 10:04pm UTC](https://forums.powershell.org/t/compare-2-csv-files/17881/3 "2021-11-05T22:04:53Z")

</div>

I edited and tried to make it clearer.

---

<div class="post-metadata">

**Author:** ![matthew](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@matthew](https://forums.powershell.org/u/matthew)\
**Post date:** [November 5, 2021, 10:28pm UTC](https://forums.powershell.org/t/compare-2-csv-files/17881/4 "2021-11-05T22:28:34Z")

</div>

> [@Olaf](#):
>
> $CSV01 = Import-Csv -Path .\file01.csv -Delimiter ‘,’  
> $CSV02 = Import-Csv -Path .\file02.csv -Delimiter ‘,’
> 
> Compare-Object -ReferenceObject $CSV01 -DifferenceObject $CSV02 -PassThru
> 
> ```auto
> 
> ```

This only works if the new line is at the end of the csv. If the new line was added before the end then the result would show the original end line as the result. I think it is only doing a count.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 5, 2021, 10:59pm UTC](https://forums.powershell.org/t/compare-2-csv-files/17881/5 "2021-11-05T22:59:17Z")

</div>

> [@matthew](#):
>
> This only works if the new line is at the end of the csv. If the new line was added before the end then the result would show the original end line as the result. I think it is only doing a count.

I’m not sure if I missunderstand you or if you missunderstand the cmdlet. 😉

1st example:

```auto
$CSV1 = 
@'
UserID,Name,Phone
1,John,1234567890
2,Jane,0987654321
50,Mary,1111111111
55,Mark,2222222222
'@ | ConvertFrom-Csv

$CSV2 = 
@'
UserID,Name,Phone
1,John,1234567890
2,Jane,0987654321
50,Mary,1111111111
55,Mark,2222222222
'@ | ConvertFrom-Csv

Compare-Object -ReferenceObject $CSV1 -DifferenceObject $CSV2 -Property UserID, Name, Phone -PassThru

```

what’s not generating any output because they are the same.

2nd example:

```auto
$CSV1 =
@'
UserID,Name,Phone
1,John,1234567890
2,Jane,0987654321
50,Mary,1111111111
55,Mark,2222222222
'@ | ConvertFrom-Csv

$CSV2 =
@'
UserID,Name,Phone
1,John,1234567890
2,Jane,0987654321
50,Mary,1111111111
'@ | ConvertFrom-Csv

Compare-Object -ReferenceObject $CSV1 -DifferenceObject $CSV2 -Property UserID, Name, Phone -PassThru

```

… what outputs this:

```auto
UserID Name Phone SideIndicator
------ ---- ----- -------------
55 Mark 2222222222 <=

```

… 3rd example:

```auto
$CSV1 =
@'
UserID,Name,Phone
1,John,1234567890
2,Jane,0987654321
50,Mary,1111111111
55,Mark,2222222222
'@ | ConvertFrom-Csv

$CSV2 =
@'
UserID,Name,Phone
1,John,1234567890
2,Max,0987654321
50,Mary,1111111111
'@ | ConvertFrom-Csv

Compare-Object -ReferenceObject $CSV1 -DifferenceObject $CSV2 -Property UserID, Name, Phone -PassThru

```

what outputs …

```auto
UserID Name Phone SideIndicator
------ ---- ----- -------------
2 Max 0987654321 =>
2 Jane 0987654321 <=
55 Mark 2222222222 <=

```

… and finally the 4th example:

```auto
$CSV1 =
@'
UserID,Name,Phone
1,John,1234567890
2,Jane,0987654321
50,Mary,1111111111
55,Mark,2222222222
'@ | ConvertFrom-Csv

$CSV2 =
@'
UserID,Name,Phone
1,John,1234567890
2,Max,0987654321
3,Jim,3333333333
50,Mary,1111111111
'@ | ConvertFrom-Csv

Compare-Object -ReferenceObject $CSV1 -DifferenceObject $CSV2 -Property UserID, Name, Phone -PassThru

```

… and the result is this output:

```auto
UserID Name Phone SideIndicator
------ ---- ----- -------------
2 Max 0987654321 =>
3 Jim 3333333333 =>
2 Jane 0987654321 <=
55 Mark 2222222222 <=

```

I think you just have to evaluate the side indicator … or do I still missunderstand it?

---

<div class="post-metadata">

**Author:** ![matthew](https://avatars.discourse-cdn.com/v4/letter/m/ed655f/32.png) [@matthew](https://forums.powershell.org/u/matthew)\
**Post date:** [November 5, 2021, 11:35pm UTC](https://forums.powershell.org/t/compare-2-csv-files/17881/6 "2021-11-05T23:35:08Z")

</div>

Thank you. That way seems to work. I will test it to make sure fully. Seems the -property is what I was missing.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [November 5, 2021, 11:36pm UTC](https://forums.powershell.org/t/compare-2-csv-files/17881/7 "2021-11-05T23:36:46Z")

</div>

Great. I’m glad if it helped. 👍🏽 😉 🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:20pm UTC](https://forums.powershell.org/t/compare-2-csv-files/17881/8 "2024-05-16T20:20:36Z")

</div>


