Compare 2 CSV file and generate results

Welcome Forums General PowerShell Q&A Compare 2 CSV file and generate results

Viewing 3 reply threads
  • Author
    Posts
    • #186248
      Participant
      Topics: 1
      Replies: 0
      Points: 19
      Rank: Member

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

      File 1

      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

      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
    • #186281
      Moderator
      Topics: 0
      Replies: 42
      Points: 388
      Team MemberHelping Hand
      Rank: Contributor

      What's your question, here?

    • #186308
      Participant
      Topics: 1
      Replies: 6
      Points: 62
      Rank: Member

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

       

      $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?

    • #186302
      Participant
      Topics: 1
      Replies: 3
      Points: 34
      Rank: Member

      Try this. I typing this on my phone so...

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

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