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. 

       -OutBuffer -OutVariable.

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       =>

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):

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

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


ForEach-Object - Loop for each object in the pipeline
New-Object - Create a new .Net object
Sort-Object - Sort the input objects by property value
Tee-Object - Send input objects to two places
Equivalent bash commands: diff3 - Show differences among three files

© Copyright 1999-2015
Some rights reserved