Compare two sets of objects e.g. compare the content within two files, one object is the reference set, one is the difference set. The result indicates where a property value appears:
only in the Reference set (<=), only in the Difference set (=>), or in both (==) when -IncludeEqual is specified.

      Compare-Object [-referenceObject] PSObject[] [-differenceObject] PSObject[] 
        [-syncWindow int] [-property Object[]] [-caseSensitive]
            [-culture string] [-excludeDifferent] [-includeEqual]
               [-passThru] [CommonParameters]

   -referenceObject PSObject[]
       Object(s) used as a reference for comparison.
   -differenceObject PSObject[]
       Object(s) to compare to the reference object(s).
   -syncWindow int
       The search region where an attempt is made to re-sync the order if there is no match.
       The Default=[Int32]::MaxValue
       (In PowerShell 1.0 this default was just 5 which is often too low)

   -property Object[]
       Properties of the objects to compare.
       Make comparisons case-sensitive.
   -culture string
       The culture to use for comparisons.
       Display only the characteristics of compared objects that are equal.
       Displays characteristics of compared objects that are equal.
       By default only differences are displayed.
       Pass the object created by this cmdlet through the pipeline.

Standard Aliases for Compare-Object: diff

Compare-Object compares two sets of objects, one the 'reference' set and the 'difference' set.

The results indicate a property value appears only in the Reference set (indicated by <= ), only in the Difference set (indicated by => ) or in both objects (indicated by == when -IncludeEqual parameter is specified.)


Compare the content of two text files:

PS C:\> "Hello World" > C:\temp\apples.txt
PS C:\> "Hello World" > C:\temp\oranges.txt
PS C:\> "More Information" >> C:\temp\oranges.txt
PS C:\> $apples = Get-Content C:\temp\apples.txt
PS C:\> $oranges = Get-Content C:\temp\oranges.txt
PS C:\> Compare-Object $apples $oranges

InputObject            SideIndicator
----------------       ------------------
More Information       =>

The SideIndicator is also available in the pipeline as $_.sideindicator

Compare the processes running before and after starting a copy of notepad.exe, using Get-Process to retrieve the processes running and store them in a variable:

PS C:\> $proc_before = get-process
PS C:\> notepad
PS C:\> $proc_after = get-process
PS C:\> compare-object -referenceobject $proc_before -differenceobject $proc_after -SyncWindow 1000

Compare the services running on 2 machines from the blog (link now 404):

PS C:\> $machine1 = Get-Service -ComputerName Server64
PS C:\> $machine2 = Get-Service -ComputerName Server65
PS C:\> Compare-Object -ReferenceObject $machine1 -DifferenceObject $machine2 -Property Name,Status `
-passThru | Sort-Object Name | Select-Object Name, Status, MachineName

Find all the users who are members of both 'group64' and 'group65', this enumerates the Member property:

$first = Get-ADGroup "group64" -Properties Member | Select-Object -ExpandProperty Member | Get-ADUser
$second = Get-ADGroup "group65" -Properties Member | Select-Object -ExpandProperty Member | Get-ADUser
compare-object ($first) ($second) -Property 'SamAccountName' -IncludeEqual -ExcludeDifferent >c:\batch\both.txt

“With the greater part of rich people, the chief enjoyment of riches consists in the parade of riches” ~ Adam Smith

Related PowerShell Cmdlets

ForEach-Object - Loop for each object in the pipeline.
Group-Object - Group the objects that contain the same value for a common property.
Measure-Object - Measure aspects of object properties and create objects from those values.
New-Object - Create a new .Net object.
Select-Object - Select objects based on parameters set in the Cmdlet command string.
Sort-Object - Sort the input objects by property value.
Tee-Object - Send input objects to two places.
Where-Object - Filter input from the pipeline allowing operation on only certain objects.
Equivalent bash commands: diff3 - Show differences among three files.

Copyright © 1999-2024
Some rights reserved