PowerTip: Use PowerShell to Compare Two Objects

PowerTip: Use PowerShell to Compare Two Objects

  • Comments 1
  • Likes

Summary: Use Windows PowerShell to compare two objects to view differences and similarities.

Hey, Scripting Guy! Question How can I compare two objects and see the values that differ and exist in both?

Hey, Scripting Guy! Answer Charlotte Windows PowerShell User Group member Brian Wilhite says: There is a cmdlet that will make life easy for you. The cmdlet is Compare-Object, and it will compare two objects and detail the results. The IncludeEqual parameter will list the properties that exist in both objects.

$OneToFive = 1..5

$OneToTen = 1..10

Compare-Object -DifferenceObject $OneToFive -ReferenceObject $OneToTen –IncludeEqual

InputObject  SideIndicator

-----------     -------------

          1         ==

          2         ==

          3         ==

          4         ==

          5         ==

          6         <=

          7         <=

          8         <=

          9         <=

         10        <=

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Could you explain why Compare-Object does not work in this (Exchange Online) scenario:

    $a = Get-Mailbox User1

    $b = Get-Mailbox User2

    Compare-Object -DifferenceObject $a -ReferenceObject $b -IncludeEqual