Compare-Object

Updated: August 9, 2015

Compare-Object

Compares two sets of objects.

Aliases

The following abbreviations are aliases for this cmdlet: 

  • compare, diff

Syntax

Parameter Set: Default
Compare-Object [-ReferenceObject] <PSObject[]> [-DifferenceObject] <PSObject[]> [-CaseSensitive] [-Culture <String> ] [-ExcludeDifferent] [-IncludeEqual] [-PassThru] [-Property <Object[]> ] [-SyncWindow <Int32> ] [ <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).

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

Parameters

-CaseSensitive

Indicates that comparisons should be case-sensitive.


Aliases

none

Required?

false

Position?

named

Default Value

none

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

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


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-IncludeEqual

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.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-PassThru

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


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

-Property<Object[]>

Specifies an array of 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[]>

Specifies an array of 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 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.


Aliases

none

Required?

false

Position?

named

Default Value

none

Accept Pipeline Input?

false

Accept Wildcard Characters?

false

<CommonParameters>

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see    about_CommonParameters.

Inputs

The input type is the type of the objects that you can pipe to the cmdlet.

  • System.Management.Automation.PSObject

    You can pipe a DifferenceObject object to this cmdlet.


Outputs

The output type is the type of the objects that the cmdlet emits.

  • 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

Example 1: Compare the content of two text files

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: Compare each line of content in two text files

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: Compare two sets of process objects

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.


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

Related topics

Community Additions

ADD
Show: