Compare-Object
Compare-Object [-ReferenceObject*] <PSObject[]> [-DifferenceObject*] <PSObject[]> [-CaseSensitive] [-Culture<String>] [-ExcludeDifferent] [-IncludeEqual] [-PassThru] [-Property <Object[]>] [-SyncWindow <Int32>][<CommonParameters>]
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>]
- Default value is False
- Accepts pipeline input False
Indicates that comparisons should be case-sensitive.
-Culture <String>
- Default value is None
- Accepts pipeline input False
Specifies the culture to use for comparisons.
-DifferenceObject <PSObject[]>
- This value is required
- Default value is None
- Accepts pipeline input ByValue
Specifies the objects that are compared to the reference objects.
-ExcludeDifferent [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Indicates that this cmdlet displays only the characteristics of compared objects that are equal.
-IncludeEqual [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
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.
-PassThru [<SwitchParameter>]
- Default value is False
- Accepts pipeline input False
Returns an object representing the item with which you are working. By default, this cmdlet does not generate any output.
-Property <Object[]>
- Default value is None
- Accepts pipeline input False
Specifies an array of properties of the reference and difference objects to compare.
-ReferenceObject <PSObject[]>
- This value is required
- Default value is None
- Accepts pipeline input False
Specifies an array of objects used as a reference for comparison.
-SyncWindow <Int32>
- Default value is None
- Accepts pipeline input False
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.
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.
Inputs
System.Management.Automation.PSObjectOutputs
None, or the objects that are differentExamples
-
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.
-
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.
-
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.
Additional Notes
This work is licensed under a Creative Commons Attribution 4.0 International. It is attributed to Microsoft Corporation and can be found here.
PowerShell Commands