Skip to content

Compare-Objects.ps1 will show you the attributes that differ between two objects. Just feed it the type of object and the names of the two you want to compare, and it will do the rest for you. Add the "-verbose" switch for more information.

License

greiginsydney/Compare-Objects.ps1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

Compare-Objects.ps1

Compare-Objects.ps1 will show you the attributes that differ between two objects. Just feed it the type of object and the names of the two you want to compare, and it will do the rest for you. Add the "-verbose" switch for more information.

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".

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

Two users?

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

VMs:

... even Disks:

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

Bugs?

If you encounter any object types or attributes that report errors, please raise an Issue 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:

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.

Revision History

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.


This script was originally published at https://greiginsydney.com/compare-objects-ps1/.

About

Compare-Objects.ps1 will show you the attributes that differ between two objects. Just feed it the type of object and the names of the two you want to compare, and it will do the rest for you. Add the "-verbose" switch for more information.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published