CmdID

CmdID / PowerShell / Cmdlets / Compare

Compare-Object

Compares two sets of objects. PowerShell reference

From the PowerShell reference 9 parameters Aliases compare, diff

Opens CmdID with your command, read as PowerShell. It is read in your browser and sent nowhere.

What it does

from the PowerShell reference, CC BY 4.0

Compares two sets of objects. The Compare-Object cmdlet compares two sets of objects. One set of objects is the reference, and the other set of objects is the difference. Compare-Object checks for available methods of comparing a whole object. If it can't find a suitable method, it calls the ToString() methods of the input objects and compares the string results. You can provide one or more properties to be used for comparison. When properties are provided, the cmdlet compares the values of those properties only. The result of the comparison indicates whether a property value appeared only in the reference object (<=) or only in the difference object (=>). If the IncludeEqual parameter is used, (==) indicates the value is in both objects. If the reference or the difference objects are null ($null), Compare-Object generates a terminating error. Some examples use splatting to reduce the line length of the code samples. For more information, see about_Splatting.

Compare-Object [-ReferenceObject] <PSObject[]> [-DifferenceObject] <PSObject[]> [-SyncWindow <Int32>] [-Property <Object[]>] [-ExcludeDifferent] [-IncludeEqual] [-PassThru] [-Culture <String>] [-CaseSensitive] [<CommonParameters>]

Example 1 - Compare the content of two text files

$objects = @{ ReferenceObject = (Get-Content -Path C:\Test\Testfile1.txt) DifferenceObject = (Get-Content -Path C:\Test\Testfile2.txt) } Compare-Object @objectsIdentify

Example 2 - Compare each line of content and exclude the differences

$objects = @{ ReferenceObject = (Get-Content -Path C:\Test\Testfile1.txt) DifferenceObject = (Get-Content -Path C:\Test\Testfile2.txt) } Compare-Object @objects -IncludeEqual -ExcludeDifferentIdentify

Example 3 - Show the difference when using the PassThru parameter

$a = $true Compare-Object -IncludeEqual $a $a (Compare-Object -IncludeEqual $a $a) | Get-MemberIdentify

Example 4 - Compare two simple objects using properties

$objects = @{ ReferenceObject = 'abc' DifferenceObject = 'xyz' Property = 'Length' } Compare-Object @objects -IncludeEqualIdentify

Example 5 - Comparing complex objects using properties

PS> Get-Process pwsh NPM(K) PM(M) WS(M) CPU(s) Id SI ProcessName ------ ----- ----- ------ -- -- ----------- 101 123.32 139.10 35.81 11168 1 pwsh 89 107.55 66.97 11.44 17600 1 pwsh PS> $a = Get-Process -Id 11168 PS> $b = Get-Process -Id 17600 PS> $a.ToString() System.Diagnostics.Process (pwsh) PS> $b.ToString() System.Diagnostics.Process (pwsh) PS> Compare-Object $a $b -IncludeEqual InputObject SideIndicator ----------- ------------- System.Diagnostics.Process (pwsh) == PS> Compare-Object $a $b -Property ProcessName, Id, CPU ProcessName Id CPU SideIndicator ----------- -- --- ------------- pwsh 17600 11.4375 => pwsh 11168 36.203125 <=Identify

Example 6 - Comparing complex objects that implement IComparable

Compare-Object ([timespan]"0:0:1") "0:0:1" -IncludeEqualIdentify

Its page in the reference: Compare-Object.

Parameters

9 in the PowerShell reference
-CaseSensitiveIndicates that comparisons should be case-sensitive.
-CultureSpecifies the culture to use for comparisons.
-DifferenceObjectSpecifies the objects that are compared to the reference objects.
-ExcludeDifferentIndicates that this cmdlet displays only the characteristics of compared objects that are equal. The differences between the objects are discarded. Use ExcludeDifferent with IncludeEqual to display only the lines that match between the reference and difference objects. If ExcludeDifferent is specified without IncludeEqual, there's no output.
-IncludeEqualIncludeEqual displays the matches between the reference and difference objects. By default, the output also includes the differences between the reference and difference objects.
-PassThruWhen you use the PassThru parameter, Compare-Object omits the PSCustomObject wrapper around the compared objects and returns the differing objects, unchanged.
-PropertySpecifies an array of properties of the reference and difference objects to compare. The value of the Property parameter can be a new calculated property. The calculated property can be a scriptblock or a hash table. Valid key-value pairs are: - Expression - <string> or <scriptblock> For more information, see about_Calculated_Properties.
-ReferenceObjectSpecifies an array of objects used as a reference for comparison.
-SyncWindowSpecifies the number of adjacent objects that Compare-Object inspects while looking for a 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. When working with large collections, the default value might not be efficient but is accurate. Specifying a smaller value for SyncWindow can increase performance but could have lower accuracy.