# Compare 2 CSV file and generate results

**URL:** <https://forums.powershell.org/t/compare-2-csv-file-and-generate-results/13368>\
**Category:** PowerShell Help\
**Created:** [November 2, 2019, 9:34pm UTC](https://forums.powershell.org/t/compare-2-csv-file-and-generate-results/13368 "2019-11-02T21:34:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![odie1415](https://avatars.discourse-cdn.com/v4/letter/o/cc9497/32.png) [@odie1415](https://forums.powershell.org/u/odie1415)\
**Post date:** [November 2, 2019, 9:34pm UTC](https://forums.powershell.org/t/compare-2-csv-file-and-generate-results/13368/1 "2019-11-02T21:34:49Z")

</div>

I am new to powershell. I am trying to compare 2 CVS and get a desired outcome.

File 1

\<colgroup\>\<col style="width: 48pt;" span="3" width="64" /\> \</colgroup\>

| UserID | Permission | Role |
| A123456 | PERM1 | Role1 |
| A123456 | PERM1 | Role2 |
| A123456 | PERM2 | Role2 |
| A123457 | PERM1 | Role2 |
| A123457 | PERM2 | Role2 |
| A123458 | PERM3 | Role3 |
| A123459 | PERM4 | Role4 |
| A123460 | PERM5 | Role5 |
| A123461 | PERM6 | Role6 |
| A123462 | PERM7 | Role7 |
| A123463 | PERM8 | Role8 |
| A123464 | PERM9 | Role9 |

File 2 \<colgroup\>\<col style="width: 48pt;" span="2" width="64" /\> \</colgroup\>

| UserID | Permission |
| A123456 | PERM1 |
| A123456 | PERM2 |
| A123457 | PERM1 |
| A123457 | PERM2 |
| A123458 | PERM3 |
| A123459 | PERM4 |
| A123460 | PERM5 |
| A123461 | PERM8 |
| A123462 | PERM7 |
| A123463 | PERM8 |
| A123464 | PERM9 |

Results

| UserID [File1] | Permission [File1] | UserID [File2] | Permission [File2] | Role [File1] | Exist on File1 (comparison if the concatanation of userid and permission from File 2 exist on File 1) | Exist on File2 (comparison if the concatanation of userid and permission from File 1 exist on File 2) | Status |
| A123456 | PERM1 | A123456 | PERM1 | Role1, Role2 | Exist on File 1 | Exist on File 2 | File 1 and File 2 Match |
| A123456 | PERM1 | A123456 | PERM1 | Role1, Role2 | Exist on File 1 | Exist on File 2 | File 1 and File 2 Match |
| A123456 | PERM2 | A123456 | PERM2 | Role2 | Exist on File 1 | Exist on File 2 | File 1 and File 2 Match |
| A123457 | PERM1 | A123457 | PERM1 | Role2 | Exist on File 1 | Exist on File 2 | File 1 and File 2 Match |
| A123457 | PERM2 | A123457 | PERM2 | Role2 | Exist on File 1 | Exist on File 2 | File 1 and File 2 Match |
| A123458 | PERM3 | A123458 | PERM3 | Role3 | Exist on File 1 | Exist on File 2 | File 1 and File 2 Match |
| A123459 | PERM4 | A123459 | PERM4 | Role4 | Exist on File 1 | Exist on File 2 | File 1 and File 2 Match |
| A123460 | PERM5 | A123460 | PERM5 | Role5 | Exist on File 1 | Exist on File 2 | File 1 and File 2 Match |
| A123461 | PERM6 | Not on File 2 | N/A | Role6 | Exist on File 1 | Does not exist on File 2 | File 1 doesn't exist on File 2 |
| A123462 | PERM7 | A123462 | PERM7 | Role7 | Exist on File 1 | Exist on File 2 | File 1 and File 2 Match |
| A123463 | PERM8 | A123463 | PERM8 | Role8 | Exist on File 1 | Exist on File 2 | File 1 and File 2 Match |
| A123464 | PERM9 | A123464 | PERM9 | Role9 | Exist on File 1 | Exist on File 2 | File 1 and File 2 Match |
| Not on File 1 | N/A | A123461 | PERM8 | N/A | Does not exist on File 1 | Exist on File 2 | File 2 doesn't exist on File 1 |

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [November 2, 2019, 9:35pm UTC](https://forums.powershell.org/t/compare-2-csv-file-and-generate-results/13368/2 "2019-11-02T21:35:05Z")

</div>

What’s your question, here?

---

<div class="post-metadata">

**Author:** ![robert\_loblaw](https://avatars.discourse-cdn.com/v4/letter/r/f17d59/32.png) [@robert\_loblaw](https://forums.powershell.org/u/robert_loblaw)\
**Post date:** [November 3, 2019, 10:24am UTC](https://forums.powershell.org/t/compare-2-csv-file-and-generate-results/13368/3 "2019-11-03T10:24:33Z")

</div>

Try this. I typing this on my phone so…

Compare-Object -ReferenceObject SOME-FILE.csv -DifferenceObject ANOTHER-FILE.csv | Out-File RESULT-FILE.txt

---

<div class="post-metadata">

**Author:** ![hef](https://avatars.discourse-cdn.com/v4/letter/h/f1d935/32.png) [@hef](https://forums.powershell.org/u/hef)\
**Post date:** [November 3, 2019, 8:57am UTC](https://forums.powershell.org/t/compare-2-csv-file-and-generate-results/13368/4 "2019-11-03T08:57:50Z")

</div>

If you just wanted to compare 2 CSVs and generate the results like your threadline says, you could do something like:

&nbsp;

```
$baselinefile = import-csv -Path "c:\delme\Test1.csv" 
$file2compare = import-csv -Path "c:\delme\Test2.csv"
Compare-Object -ReferenceObject $baselinefile -DifferenceObject $file2compare -Property Name, DisplayName, Status, State -IncludeEqual | ft
```

In the above example I had pulled a list of services from my machine and tossed them in file 1, then I stopped a service and did it again in file 2. The fields I’m looking at are DisplayName, Status, and State (you could change that to whatever fields were in your CSV that you wanted to work with). I told it to include the equal items in the output (you could omit that if you wanted and only see the lines that don’t match). The last item was piping it into a format-table (short hand is just ft) to change how the object looks. The output will have an additional line called “SideIndicator” which will tell you if things match or not.

All that being said, looking at what you posted in your 3rd output block, it sort of looks like you were steering towards more customized output. This is probably why the moderator asked you what your question was exactly. Your thread title, and your requested output don’t precisely match and it would be a guessing game as to which direction you’re trying to go. To do that kind of custom output (off the top of my head) I’d probably have to create some arrays, loop through them making comparisons, and writing the custom output into a new file. That would be a bit more code than I’m showing above, you’d need to give some indication of what you know how to do and don’t, while laying out a little of the code up front so I’m not doing it all for you from scratch and am only helping you with learning what you’re missing.

So… what was your real question, or did the compare-object cmdlet answer it?

---

<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:31pm UTC](https://forums.powershell.org/t/compare-2-csv-file-and-generate-results/13368/5 "2024-05-16T20:31:11Z")

</div>

Closed on May 16, 2024, 8:31pm UTC


