ISSUE
Compare-Object falsely reports some of the the same objects as being different.
For example, you compare two sets of objects:
$set1 = @(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
$set2 = @(15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
Compare-Object $set1 $set2
And see that the output falsely reports 1, 2, 14, and 15 being different (and in both sets.)
InputObject SideIndicator----------- -------------15 =>1 <=14 =>2 <=2 =>1 =>14 <=15 <=
CAUSE
The root of the issue is in the -SyncWindow parameter of Compare-Object. This parameter tells the cmdlet how far around to look to find the same element. By default it equals 5 – meaning that PowerShell will look at the item and plus/minus 5 elements around – which means that the defaults are good if you compare collections up to 11 elements. Anything bigger (as in our example) requires a bigger window (unless the collections are sorted).
RESOLUTION
Explicitly set sync window to half of the size of the smaller collection. In our case, the collections have 15 elements, so sync window should be set to 7:
Compare-Object $set1 $set2 -SyncWindow 7
This is a fairly frequent issue which people are facing for example when comparing group membership between different groups or a group and a CSV file.
Tags: KB, Knowledge Base, Known Issues, PowerShell
Subscribe by email




Thank you! That confused me for the longest time.