Compare-Object

Compares two sets of objects.
Compare-Object [-ReferenceObject*] <PSObject[]> [-DifferenceObject*] <PSObject[]> [-CaseSensitive] [-Culture[<String>]] [-ExcludeDifferent] [-IncludeEqual] [-PassThru] [-Property [<Object[]>]] [-SyncWindow [<Int32>]][<CommonParameters>]

The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference set, and the other set is the difference set.

The result of the comparison indicates whether a property value appeared only in the object from the reference set (indicated by the symbol) or, if the IncludeEqual parameter is specified, in both objects (indicated by the == symbol).

If the reference set or the difference set is null, this cmdlet generates a terminating error.

Parameters
-CaseSensitive [<SwitchParameter>]

Indicates that comparisons should be case-sensitive.

-Culture [<String>]

Specifies the culture to use for comparisons.

-DifferenceObject <PSObject[]>

  • This value is required
  • Accepts pipeline input ByValue

Specifies the objects that are compared to the reference objects.

-ExcludeDifferent [<SwitchParameter>]

Indicates that this cmdlet displays only the characteristics of compared objects that are equal.

-IncludeEqual [<SwitchParameter>]

Indicates that this cmdlet displays characteristics of compared objects that are equal. By default, only characteristics that differ between the reference and difference objects are displayed.

-PassThru [<SwitchParameter>]

Returns an object representing the item with which you are working. By default, this cmdlet does not generate any output.

-Property [<Object[]>]

Specifies an array of properties of the reference and difference objects to compare.

-ReferenceObject <PSObject[]>

  • This value is required

Specifies an array of objects used as a reference for comparison.

-SyncWindow [<Int32>]

Specifies the number of adjacent objects that this cmdlet inspects while looking for a match in a collection of objects. This cmdlet examines adjacent objects when it does not find the object in the same position in a collection. The default value is [Int32]::MaxValue, which means that this cmdlet examines the entire object collection.

<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.

Inputs

System.Management.Automation.PSObject

You can pipe a DifferenceObject object to this cmdlet.

Outputs

None, or the objects that are different

When you use the PassThru parameter, Compare-Object returns the objects that differed. Otherwise, this cmdlet does not generate any output.

Examples
  1. Compare the content of two text files:
    PS C:> Compare-Object -ReferenceObject $(Get-Content C:testtestfile1.txt) -DifferenceObject $(Get-Content C:testtestfile2.txt)
    

    This command compares the contents of two text files. It displays only the lines that appear in one file or in the other file, not lines that appear in both files.

  2. Compare each line of content in two text files:
    PS C:> Compare-Object -ReferenceObject $(Get-Content C:Testtestfile1.txt) -DifferenceObject $(Get-Content C:Testtestfile2.txt) -IncludeEqual
    

    This command compares each line of content in two text files. It displays all lines of content from both files, indicating whether each line appears in only Textfile1.txt or Textfile2.txt or whether each line appears in both files.

  3. Compare two sets of process objects:
    PS C:> $Processes_Before = Get-Process
    PS C:> notepad
    PS C:> $Processes_After = Get-Process
    PS C:> Compare-Object -ReferenceObject $Processes_Before -DifferenceObject $Processes_After
    

    These commands compare two sets of process objects.

    The first command uses the Get-Process cmdlet to get the processes on the computer. The cmdlet stores the processes in the $Processes_Before variable.

    The second command launches Notepad.

    The third command uses the Get-Process cmdlet again and stores the resulting processes in the $Processes_After variable.

    The fourth command uses the Compare-Object cmdlet to compare the two sets of process objects. It displays the differences between them, which include the new instance of Notepad.

Related Links

ForEach-Object
Group-Object
Measure-Object
New-Object
Select-Object
Sort-Object
Tee-Object
Where-Object
Get-Process