Compare-Objects.ps1

Hands up if you’ve tried to compare 2 objects of some type to see what – if any – differences there are between them?

I tried and gave up. PowerShell’s native “Compare-Object” isn’t very helpful. It will tell you IF there’s a difference, but it’s not particularly forthcoming.

Borne of that experience comes “Compare-Objects.ps1”. You might see some similarities here with two of my other scripts (Compare-PkiCertificates & Update-SfbCertificate.ps1) as the comparison engine is essentially the same between them.

Feed this script the “type” of the object and the names of two of them, and it will present a tabular comparison, highlighting all those attributes that differ.

All of these formats are valid input examples:

Compare-Objects.ps1 –type csuser –object1 “greig” –object2 “jessica”
Compare-Objects.ps1 –type csuser –object1 greig –object2 jessica
Compare-Objects.ps1 get-csuser greig jessica

Armed with the above input the script performs two “get-” commands to query the objects, then feeds the results into the differencing engine. The “get-” is implied in the command-line input, and the script will cope fine if you absent-mindedly include it, like in the last example above.

If you’ve already captured the objects, you can feed them to the script and it will compare them directly, skipping the “get” step:

Compare-Objects.ps1 –type $null –object1 $greig –object2 $jessica
Compare-Objects.ps1 $null $greig $jessica

For more information add the “-verbose” switch, and if you don’t want it querying my blog in search of an update, use “-SkipUpdateCheck”.

Examples

One of my earliest requirements was to compare client policies in Skype for Business. Here’s the script doing that for me, condensing 86 attributes down to the 4 that differ:

PS: C:\>.\Compare-Objects.ps1 csclientpolicy Hotdesking SkypeUI -Verbose

Compare-CsClientPolicy

Two users?

PS: C:\>.\Compare-Objects.ps1 csuser greig jessica

Compare-CsUser

VMs:

Compare-vm

… even Disks:

PS:>.\Compare-Objects.ps1 -Type disk 0 1 -Verbose

Compare-Disk

Bugs?

If you encounter any object types or attributes that report errors, please let me know in the comments here and I’ll do my best to cater for them.

I will admit to drawing a blank with a couple of Exchange attributes, as you can see from the errors thrown when I compare two mailbox databases:

Compare-MailboxDatabases
If you’re seeing errors and are curious, adding the “-debug” switch will dump more diagnostic info.

The Wheel, reinvented

In the process of writing this post I stumbled upon Jamie Nelson’s post on the TechNet blog “Compare all properties of two objects in Windows PowerShell”, in which he provides a neat function in 22 lines of code that basically does all of the above. Yes, I cursed a little at that belated finding, but kudos Jamie for stepping into the breach.

Download

You’ll find a code-signed version of the script on GitHub. You’re welcome to pinch, adapt or improve upon the code with my blessing. If you encounter any problems with it please create an issue on the repo.

It has an update checking component built-in so it will let you know as updates become available. You can suppress the update check by running it with the “-SkipUpdateCheck” parameter.

Revision History

6th June 2020. Updated the Download link to point to GitHub.

v1.1 – 20th May 2018

  • When queried, the PowerShell ISE reports the screen width is 0. Script now checks for zero and forces width to 80
  • Added override switch ‘-width’ for extra user control. I assume this is only ever going to be needed by die-hard ISE users ;-)
  • Allowed the script to accept a -type of $null so the user can pass in entire objects to be compared, rather than just strings (see examples)
  • Added a ‘select-object’ to only compare the FIRST instance if what’s passed in/returned is an array of more than 1 object, and displays a warning
  • Added pipeline handling so you can pipe objects to the script (run get-help -full)

v1.0 – 5th May 2018. This is the initial release.
 

– G.

Leave a Reply

Your email address will not be published.

... and please just confirm for me that you're not a bot first: Time limit is exhausted. Please reload the CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.