Compare-Object 2 CSV sheets to output similarities

This topic contains 3 replies, has 2 voices, and was last updated by Profile photo of Dave Wyatt Dave Wyatt 2 years, 1 month ago.

  • Author
    Posts
  • #20264
    Profile photo of Network Operations
    Network Operations
    Participant

    I'm attempting to build a PS script to check usernames on two different CSV sheets. One has disabled users, the other has licensed, enabled users. I need to find out if any of the licensed, enabled users appear on the disabled users CSV sheet. I've gotten thus far:

    compare-object (get-content $File1) (get-content $File2) | format-list | Out-File $Location

    But I'm not sure how to get this down just right.

  • #20268
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    You'll probably have a better time using Import-Csv, rather than Get-Content. That way you wind up with objects and can make use of the -Property parameter of Compare-Object. Since you're only interested in users that are in both files, you can use the -IncludeEqual and -ExcludeDifferent switches as well. Something along these lines, assuming that "UserName" is a column name in your files:

    Compare-Object (Import-Csv $File1) (Import-Csv $File2) -Property UserName -IncludeEqual -ExcludeDifferent
    
  • #20289
    Profile photo of Network Operations
    Network Operations
    Participant

    This is exactly what I needed. Thank you!

  • #20290
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    No problem, happy to help! 🙂

You must be logged in to reply to this topic.