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 my 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”.
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
PS: C:\>.\Compare-Objects.ps1 csuser greig jessica
… even Disks:
PS:>.\Compare-Objects.ps1 -Type disk 0 1 -Verbose
If you encounter any object types or attributes that report errors, please let me know in the questions here (or in the comments back on my blog) 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:
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.
v1.1 – 20th May 2018
v1.0 - 5th May 2018. This is the initial release.