< Back

Compare-Object

Tue Jan 29, 2019 10:21 pm

NAME Compare-Object



SYNOPSIS

Compares two sets of objects.





SYNTAX

Compare-Object [-ReferenceObject] <PSObject[]> [-DifferenceObject] <PSObject[]> [-CaseSensitive] [-Culture <String>] [-ExcludeDifferent]

[-IncludeEqual] [-PassThru] [-Property <Object[]>] [-SyncWindow <Int32>] [<CommonParameters>]





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 ($null), this cmdlet generates a terminating error.





PARAMETERS

-CaseSensitive [<SwitchParameter>]

Indicates that comparisons should be case-sensitive.



Required? false

Position? named

Default value False

Accept pipeline input? False

Accept wildcard characters? false



-Culture <String>

Specifies the culture to use for comparisons.



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.



Required? true

Position? 1

Default value None

Accept pipeline input? True (ByValue)

Accept wildcard characters? false



-ExcludeDifferent [<SwitchParameter>]

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



Required? false

Position? named

Default value False

Accept pipeline input? False

Accept wildcard characters? false



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



Required? false

Position? named

Default value False

Accept pipeline input? False

Accept wildcard characters? false



-PassThru [<SwitchParameter>]

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



Required? false

Position? named

Default value False

Accept pipeline input? False

Accept wildcard characters? false



-Property <Object[]>

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



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.



Required? true

Position? 0

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.



Required? false

Position? named

Default value None

Accept pipeline input? False

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).



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.





NOTES









Example 1: Compare the content of two text files



PS C:\\>Compare-Object -ReferenceObject $(Get-Content C:\\test\\testfile1.txt) -DifferenceObject $(Get-Content C:\\test\\testfile2.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.

Example 2: Compare each line of content in two text files



PS C:\\>Compare-Object -ReferenceObject $(Get-Content C:\\Test\\testfile1.txt) -DifferenceObject $(Get-Content C:\\Test\\testfile2.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.

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



RELATED LINKS

Online Version: http://go.microsoft.com/fwlink/?LinkId=821751

Group-Object

Measure-Object

New-Object

Select-Object

Sort-Object

Tee-Object