Compares two sets of objects.
Syntax
Copy Code | |
---|---|
Compare-Object [-ReferenceObject] <PSObject[]> [-DifferenceObject] <PSObject[]> [-CaseSensitive] [-Culture <string>] [-ExcludeDifferent] [-IncludeEqual] [-PassThru] [-Property <Object[]>] [-SyncWindow <int>] [<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).
Parameters
-CaseSensitive
Indicates that comparisons should be case-sensitive.
Required? |
false |
Position? |
named |
Default Value |
|
Accept Pipeline Input? |
false |
Accept Wildcard Characters? |
false |
-Culture <string>
Specifies the culture to use for comparisons.
Required? |
false |
Position? |
named |
Default Value |
|
Accept Pipeline Input? |
false |
Accept Wildcard Characters? |
false |
-DifferenceObject <PSObject[]>
Specifies the objects that are compared to the reference objects.
Required? |
true |
Position? |
2 |
Default Value |
|
Accept Pipeline Input? |
true (ByValue) |
Accept Wildcard Characters? |
false |
-ExcludeDifferent
Displays only the characteristics of compared objects that are equal.
Required? |
false |
Position? |
named |
Default Value |
|
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.
Required? |
false |
Position? |
named |
Default Value |
|
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.
Required? |
false |
Position? |
named |
Default Value |
|
Accept Pipeline Input? |
false |
Accept Wildcard Characters? |
false |
-Property <Object[]>
Specifies the properties of the reference and difference objects to compare.
Required? |
false |
Position? |
named |
Default Value |
|
Accept Pipeline Input? |
false |
Accept Wildcard Characters? |
true |
-ReferenceObject <PSObject[]>
Objects used as a reference for comparison.
Required? |
true |
Position? |
1 |
Default Value |
|
Accept Pipeline Input? |
false |
Accept Wildcard Characters? |
false |
-SyncWindow <int>
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.
Required? |
false |
Position? |
named |
Default Value |
[Int32]::MaxValue |
Accept Pipeline Input? |
false |
Accept Wildcard Characters? |
false |
<CommonParameters>
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -OutBuffer, -OutVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
Inputs and Outputs
The input type is the type of the objects that you can pipe to the cmdlet. The return type is the type of the objects that the cmdlet returns.
Inputs |
System.Management.Automation.PSObject You can pipe a DifferenceObject object to Compare-Object. |
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. |
Example 1
Copy Code | |
---|---|
C:\PS>compare-object -referenceobject $(get-content C:\test\testfile1.txt) -differenceobject $(get-content C:\test\testfile2.txt) Description ----------- 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
Copy Code | |
---|---|
C:\PS>compare-object -referenceobject $(get-content C:\Test\testfile1.txt) -differenceobject $(get-content C:\Test\testfile2.txt) -includeequal Description ----------- 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
Copy Code | |
---|---|
C:\PS>$processes_before = get-process C:\PS> notepad C:\PS> $processes_after = get-process C:\PS> compare-object -referenceobject $processes_before -differenceobject $processes_after Description ----------- 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. |