PowerShell Genie

Compare-Object

Use Compare-Object to compare two sets of objects and report which items are only in one of them.

Command 1 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn — Compare-Object

Syntax

1PS> Compare-Object [-ReferenceObject] <PSObject[]> [-DifferenceObject] <PSObject[]> [-Property <Object[]>] [-IncludeEqual] [-ExcludeDifferent] [-PassThru] [-CaseSensitive] [<CommonParameters>]
See the command anatomy

Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

Compare-Object [-ReferenceObject] <PSObject[]> [-DifferenceObject] <PSObject[]> [-Property <Object[]>] [-IncludeEqual] [-ExcludeDifferent] [-PassThru] [-CaseSensitive] [<CommonParameters>]
  1. Stage 1Compare-Object-ReferenceObject -DifferenceObject -Property -IncludeEqual -ExcludeDifferent -PassThru -CaseSensitive

    Try it

    Windows PowerShell

    PowerShell example · browser support is limited.

    Execution scope: The full example below requires PowerShell outside this browser. You can edit this runner to use the supported practice commands; unsupported syntax returns an explanation.

    Interactive simulation · fictional data

    Examples

    Compare-Object -ReferenceObject

    1PS> Compare-Object -ReferenceObject @('a','b','c') -DifferenceObject @('b','c','d')
    Show reference output

    Sample output · fictional data, shaped like the real thing

    InputObject  SideIndicator
    -----------  -------------
    SRV03        =>
    SRV01        <=

    Compare-Object -ReferenceObject (2)

    1PS> Compare-Object -ReferenceObject @('a','b') -DifferenceObject @('b','c') -IncludeEqual
    Show reference output

    Sample output · fictional data, shaped like the real thing

    InputObject  SideIndicator
    -----------  -------------
    SRV03        =>
    SRV01        <=

    Parameters

    Parameter definitions:

    • -ReferenceObject
    • -DifferenceObject
    • -Property
    • -IncludeEqual
    • -ExcludeDifferent
    • -PassThru
    • -CaseSensitive

    Compare-Object on Microsoft Learn

    Practical use and troubleshooting

    Practical scenario: Use Compare-Object when you need to compare two sets of objects and report which items are only in one of them. Start with one identified target and inspect the returned values before extending the operation to a list. Keep the identity in your report so you can trace each result back to the object or path that produced it.

    Common error and fix: Unexpected differences can arise from comparing object representations rather than the intended keys. Specify -Property and inspect SideIndicator to distinguish the reference set from the difference set. In an unattended script, capture this failure with its original error record and record which target failed. An empty result and an unsuccessful query must remain distinguishable in the final report.

    See also: Sort-Object. Compare-Object identifies differences between two input sets; Sort-Object orders a set. Choose key properties for comparison rather than expecting a visually sorted list to establish equality. Read the linked command before substituting it; similar names do not imply identical inputs or output.

    Compare-Object reference on Microsoft Learn

    Recap

    Compare-Object Use Compare-Object to compare two sets of objects and report which items are only in one of them.

    Next: Select-Object
    Genie guideCompare-Object

    Use Compare-Object to compare two sets of objects and report which items are only in one of them. The console below is pre-loaded — run it, then change a value and run it again.