Compare-Object

Mastering the Power of Compare-Object in PowerShell

Welcome back to Wahmans PowerShell blog! Today we’re diving into a very powerful cmdlet: Compare-Object. As the name suggests, this cmdlet allows you to compare two sets of objects — a versatile tool that you can use in script testing, configuration validation, and much more.

According to Microsoft, Compare-Object “compares two sets of objects.” It returns the differences between the two sets — a must-have in your PowerShell toolbox.

Basic Syntax

Compare-Object -ReferenceObject <ObjectSet1> -DifferenceObject <ObjectSet2>

The result includes a side indicator (<= or =>) showing which object set the item belongs to.


Use Case #1: Comparing Two Simple Arrays

Let’s start simple. Suppose we have two arrays of number values:

$array1 = 1, 2, 3, 4
$array2 = 3, 4, 5, 6

Compare-Object -ReferenceObject $array1 -DifferenceObject $array2

This will output the differences:

  • 1 and 2 are only in $array1 (<=)
  • 5 and 6 are only in $array2 (=>)

Use Case #2: Comparing Content of Two Text Files

If you have two configuration files and you’d like to know what changed, it’s very efficient to compare file content:

$file1 = Get-Content -Path "C:\configs\old-config.txt"
$file2 = Get-Content -Path "C:\configs\new-config.txt"

Compare-Object -ReferenceObject $file1 -DifferenceObject $file2

This helps highlight lines that were added or removed between versions.


Use Case #3: Verifying Installed Software Between Two Machines

Let’s say you want to compare installed software on two systems:

$pc1 = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object -ExpandProperty DisplayName
$pc2 = Get-ItemProperty \\RemotePC\HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object -ExpandProperty DisplayName

Compare-Object -ReferenceObject $pc1 -DifferenceObject $pc2

This command compares installed programs and helps identify discrepancies between systems.


Use Case #4: Structural Comparison of Custom Objects

Let’s get advanced by comparing actual object properties:

$userList1 = @(
  [PSCustomObject]@{Name='Alice'; Department='IT'},
  [PSCustomObject]@{Name='Bob'; Department='HR'}
)

$userList2 = @(
  [PSCustomObject]@{Name='Alice'; Department='IT'},
  [PSCustomObject]@{Name='Charlie'; Department='Finance'}
)

Compare-Object -ReferenceObject $userList1 -DifferenceObject $userList2 -Property Name, Department

This gives you a property-level difference. Great for auditing systems or checking changes after importing CSV files.


Bonus Tip: Add -IncludeEqual to show matching items or -PassThru to pass objects through the pipeline!

Wrap Up

As you can see, Compare-Object is more than just a comparison tool — it’s an essential cmdlet for any automation or administrative task in PowerShell.

Happy scripting, and I will see you in the next post!

Leave a Reply

Your email address will not be published. Required fields are marked *