Comparing Results

by Feb 23, 2009

PowerShell makes it easy to compare results and find only things that changed. For example, you may want to list only processes that started after a given time. To do that, first create the initial snapshot of running processes. Then, at a later time, create another snapshot and compare both using compare-object:

$base = Get-Process
$compare = Get-Process

Compare-Object $base $compare

Compare-Object only lists those objects that exist in only one of the snapshots. A "side indicator" indicates whether the object existed in the first or in the second result set.

$shot1 = Dir $home
Set-Content $hometestfile1.txt "A new file"
$shot2 = Dir $home
Compare-Object $shot1 $shot2

Compare-Object will return wrong results when there are more than 10 consecutive differences in both snapshots because it uses an internal "SyncWindow" of +- 5 elements. In this case, increase the sync window using the -syncWindow parameter:

Compare-Object $shot1 $shot2 -syncWindow 20