Cmdlets

Choose a cmdlet from the list on the left or search for a specific cmdlet. Choose a cmdlet from the list or search for a specific cmdlet.
About Help  Providers
 

Compare-Object

Compare-Object

microsoft.powershell.commands.utility.dll

Synopsis

Compares two sets of objects.

Syntax

Compare-Object [-ReferenceObject] [-DifferenceObject] [-CaseSensitive] [-Culture] [-ExcludeDifferent] [-IncludeEqual] [-PassThru] [-Property] [-SyncWindow] [<CommonParameters>]

Detailed Description

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), only in the object from the difference set (indicated by the => symbol) or, if the IncludeEqual parameter is specified, in both objects (indicated by the == symbol).

NOTE: If the reference set or the difference set is null ($null), Compare-Object generates a terminating error.

Parameters

-CaseSensitive <SwitchParameter>

Indicates that comparisons should be case-sensitive.

Aliases

None

Required?

false

Position

named

Default value

False

Accept pipeline input?

false

Accept wildcard characters?

false

-Culture <String>

Specifies the culture to use for comparisons.

Aliases

None

Required?

false

Position

named

Default value

None

Accept pipeline input?

false

Accept wildcard characters?

false

-DifferenceObject <PSObject[]>

Specifies the objects that are compared to the reference objects.

Aliases

None

Required?

true

Position

2

Default value

None

Accept pipeline input?

true (ByValue)

Accept wildcard characters?

false

-ExcludeDifferent <SwitchParameter>

Displays only the characteristics of compared objects that are equal.

Aliases

None

Required?

false

Position

named

Default value

False

Accept pipeline input?

false

Accept wildcard characters?

false

-IncludeEqual <SwitchParameter>

Displays characteristics of compared objects that are equal. By default, only characteristics that differ between the reference and difference objects are displayed.

Aliases

None

Required?

false

Position

named

Default value

False

Accept pipeline input?

false

Accept wildcard characters?

false

-PassThru <SwitchParameter>

Passes the objects that differed to the pipeline. By default, this cmdlet does not generate any output.

Aliases

None

Required?

false

Position

named

Default value

False

Accept pipeline input?

false

Accept wildcard characters?

false

-Property <Object[]>

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

Aliases

None

Required?

false

Position

named

Default value

None

Accept pipeline input?

false

Accept wildcard characters?

false

-ReferenceObject <PSObject[]>

Objects used as a reference for comparison.

Aliases

None

Required?

true

Position

1

Default value

None

Accept pipeline input?

false

Accept wildcard characters?

false

-SyncWindow <Int32>

Specifies the number of adjacent objects that Compare-Object inspects while looking for an match in a collection of objects. Compare-Object examines adjacent objects when it doesn't find the object in the same position in a collection. The default value is [Int32]::MaxValue, which means that Compare-Object examines the entire object collection.

Aliases

None

Required?

false

Position

named

Default value

[Int32]::MaxValue

Accept pipeline input?

false

Accept wildcard characters?

false

Input Type

System.Management.Automation.PSObject

Return Type

None, or the objects that are different

Notes

None

Examples

-------------------------- EXAMPLE 1 --------------------------

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.

PS C:\>compare-object -referenceobject $(get-content C:\test\testfile1.txt) -differenceobject $(get-content C:\test\testfile2.txt)

-------------------------- EXAMPLE 2 --------------------------

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.

PS C:\>compare-object -referenceobject $(get-content C:\Test\testfile1.txt) -differenceobject $(get-content C:\Test\testfile2.txt) -includeequal

-------------------------- EXAMPLE 3 --------------------------

These commands compare two sets of process objects.

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

The second command starts 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 displaysthe differences between them, which include the new instance of Notepad.

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

Online Version
ForEach-Object
Group-Object
Measure-Object
New-Object
Select-Object
Sort-Object
Tee-Object
Where-Object