Powershell – Compare-Object gives false differences

powershell

I have some problem with Compare-Object. My task is to get difference
between two directory snapshots made at different times. First
snapshot is taken like this:

ls -recurse d:\dir | export-clixml dir-20100129.xml 

Then, later, I get second snapshot and load both of them:

$b = (import-clixml dir-20100130.xml) 
$a = (import-clixml dir-20100129.xml) 

Next, I'm trying to compare with Compare-Object, like that:

diff $a $b 

What I get is in some places files that were added to $b since $a, but
in some — files that were in both snapshots, and some files, that
were added to $b, are not given in Compare-Object output. Puzzling,
but $b.count – $a.count is EXACTLY the same as (diff $a $b).count. Why
is that?

Ok, Compare-Object has -property param. I try to use that:

diff -property fullname $a $b 

And I get the whole mess of differences: it shows me ALL the files.
For example, say $a contains:

A\1.txt 
A\2.txt 
A\3.txt 

And $b contains:

X\2.mp3 
X\3.mp3 
X\4.mp3 
A\1.txt 
A\2.txt 
A\3.txt 

diff output is something like that:

X\2.mp3 => 
A\1.txt <= 
X\3.mp3 => 
A\2.txt <= 
X\4.mp3 => 
A\3.txt <= 
A\1.txt => 
A\2.txt => 
A\3.txt => 

Weird. I think I don't understand something crucial about Compare-
Object usage, and manuals are scarce… Please, help me to get the
DIFFERENCE between two directory snapshots. Thanks in advance

UPDATE: I've saved data as plain strings like that:

> import-clixml dir-20100129.xml | % { $_.fullname } | out-file -enc utf8 a.txt

And results are the same. Here're excerpts of both snapshots (top 100-something lines, a.txt and b.txt), output of compare-object, and output of UNIX diff (unified). All files are UTF-8: http://dl.dropbox.com/u/2873752/compare-object-problem.zip

Best Answer

One can solve the problem by using -SyncWindow parameter. The value should be half a length of a smallest collection in comparison:

> Compare-Object -SyncWindow ($a.length / 2) $a $b

Source: http://dmitrysotnikov.wordpress.com/2008/06/06/compare-object-gotcha/

Related Topic