Compare-Object
Compares two sets of objects.
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) 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 [<SwitchParameter>]
|
-Culture [<String>]
|
-DifferenceObject <PSObject[]>
|
-ExcludeDifferent [<SwitchParameter>]
|
-IncludeEqual [<SwitchParameter>]
|
-PassThru [<SwitchParameter>]
|
-Property [<Object[]>]
|
-ReferenceObject <PSObject[]>
|
-SyncWindow [<Int32>]
|
<CommonParameters>
|
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.
Examples
- Compare the content of two text files:
PS C:> Compare-Object -ReferenceObject $(Get-Content C:testtestfile1.txt) -DifferenceObject $(Get-Content C:testtestfile2.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:Testtestfile1.txt) -DifferenceObject $(Get-Content C:Testtestfile2.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. 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.
Related Links
ForEach-Object
Group-Object
Measure-Object
New-Object
Select-Object
Sort-Object
Tee-Object
Where-Object
Get-Process