Export (0) Print
Expand All
Expand Minimize

Compare-Object

Updated: December 3, 2014

Applies To: Windows PowerShell 4.0

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

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

Parameters

-CaseSensitive

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

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

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

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

<CommonParameters>

This cmdlet supports the common parameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, and -OutVariable. For more information, see  about_CommonParameters (http://go.microsoft.com/fwlink/p/?LinkID=113216).

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 Compare-Object.


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

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

Related topics



Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft