Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


And $b contains:


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:

share|improve this question
up vote 2 down vote accepted

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


share|improve this answer
Note that as of PowerShell v2, the -SyncWindow property has a default value of [int32]::MaxValue. – Bacon Bits May 8 '15 at 14:01

This is due to the export-clixml/import-clixml. It actually changes the type of each file object from System.IO.FileSystemInfo to Deserialized.System.IO.FileInfo.

Have your second ls command export-clixml to a temporary file, then import and compare against that and it should fix the problem.

share|improve this answer
Actually, both snapshot are from Import-Clixml, sorry for misleading. I've corrected the question. But it has no effect, actually -- result is the same. – Andrey Balaguta Jan 30 '10 at 8:37

A good example that I've found is this.

Get the original status for services:

$before = get-service

Now make some changes to the service, for example shut down 1 and start 1.

Get the new status for services:

$after = get-service

Then compare the two lists (diff is an alias for Compare-Object):

diff $before $after -property name,status

Examples results:

PS> diff $before $after -property name,status

name                                                                     status SideIndicator
----                                                                     ------ -------------
Spooler                                                                 Stopped =>
Spooler                                                                 Running <=

Demonstrates 1st object with the <=, 2nd object of the change seen with a =>. Or simply a before and after perspective. You could also create a label and expression to show the word "Changed" in the event there is a =>.

Using this you can also see in a comparison when something is in the first list but was in the second. Much like a find unmatch query.

share|improve this answer
To further improve the answer, try using the same context (a directly list) to answer the question. – Bernie White Nov 5 '12 at 10:09

Personally, i find that diff works better if you can sort it first


$b = (import-clixml dir-20100130.xml) | sort
$a = (import-clixml dir-20100129.xml) | sort
diff $a $b
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.