PowerShell Compare-Object – How to Use

In this tutorial, I will explain how to use the PowerShell Compare-Object cmdlet. Recently, while working on a project for a client in San Francisco, I encountered a scenario where I needed to compare two sets of user data to identify discrepancies. This tutorial will help you understand how to use Compare-Object in PowerShell.

To use the PowerShell Compare-Object cmdlet, you compare two sets of objects to identify differences and similarities. For example, to compare two lists of user names, you can use Compare-Object -ReferenceObject $List1 -DifferenceObject $List2. This command will highlight which items are unique to each list, helping you quickly identify discrepancies.

PowerShell Compare-Object

PowerShell’s Compare-Object cmdlet is used to compare two sets of objects. Whether you are comparing files, directories, or custom objects, Compare-Object can help you identify differences and similarities efficiently.

Now, let me show you different examples of Compare-Object in PowerShell.

PowerShell Compare-Object Syntax

The basic syntax of Compare-Object is:

Compare-Object -ReferenceObject <Object1> -DifferenceObject <Object2>

Here are a few examples.

Example 1: Compare User Lists

Let’s say you have two lists of user names from different sources:

$ADUsers = @("John Smith", "Jane Doe", "Michael Johnson", "Emily Davis")
$HRUsers = @("John Smith", "Jane Doe", "Michael Johnson", "Sarah Brown")

To compare these lists, you can use the PowerShell Compare-Object cmdlet below.

Compare-Object -ReferenceObject $ADUsers -DifferenceObject $HRUsers

This command will output the differences between the two lists, helping you identify which users are present in one list but not the other.

I executed the above PowerShell script, and you can see the exact output in the screenshot below:

How to Use PowerShell Compare-Object

Check out PowerShell Export-CSV cmdlet

Example 2: Compare Files

Suppose you have two configuration files for servers in Los Angeles and Chicago. To compare these files, you can use:

Compare-Object -ReferenceObject (Get-Content "C:\Configs\LA_Config.txt") -DifferenceObject (Get-Content "C:\Configs\Chicago_Config.txt")

This will show you the differences between the two configuration files.

Example 3: Using the -IncludeEqual Parameter

If you want to see both differences and similarities, you can use the -IncludeEqual parameter:

$ADUsers = @("John Smith", "Jane Doe", "Michael Johnson", "Emily Davis")
$HRUsers = @("John Smith", "Jane Doe", "Michael Johnson", "Sarah Brown")
Compare-Object -ReferenceObject $ADUsers -DifferenceObject $HRUsers -IncludeEqual

This will include a side indicator (<=, =>, ==) to show whether the object is present in the reference, difference, or both.

You can see the exact output in the screenshot below:

Compare-Object in PowerShell

Check out PowerShell Import-Csv Cmdlet

Example 4: Compare Custom Objects

Consider you have two lists of custom objects representing user data:

$ADUsers = @(
    [PSCustomObject]@{Name="John Smith"; Department="IT"},
    [PSCustomObject]@{Name="Jane Doe"; Department="HR"},
    [PSCustomObject]@{Name="Michael Johnson"; Department="Finance"}
)

$HRUsers = @(
    [PSCustomObject]@{Name="John Smith"; Department="IT"},
    [PSCustomObject]@{Name="Jane Doe"; Department="HR"},
    [PSCustomObject]@{Name="Sarah Brown"; Department="Finance"}
)

To compare these objects based on the Name property, you can use:

Compare-Object -ReferenceObject $ADUsers -DifferenceObject $HRUsers -Property Name

This will compare the Name property of each object and highlight differences.

I executed the above PowerShell script, and you can see the exact output in the screenshot below:

PowerShell Compare-Object

Check out PowerShell Set-Content cmdlet

Example 5: Synchronize Differences

You can use Compare-Object in conjunction with other cmdlets to synchronize differences. For example, to find users in the HR list but not in the AD list and add them to AD:

$differences = Compare-Object -ReferenceObject $ADUsers -DifferenceObject $HRUsers -Property Name -PassThru | Where-Object { $_.SideIndicator -eq "=>" }
foreach ($user in $differences) {
    # Add user to AD
    New-ADUser -Name $user.Name -Department $user.Department
}

Conclusion

PowerShell Compare-Object cmdlet is used for data comparison tasks. In this tutorial, I explained how to use the Compare-Object PowerShell cmdlet.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.