Compare Two Directories with Powershell

We use DFS to keep webfarm nodes’ content in sync and ran across a problem where a directory with thousands of files and folders had one missing file on a replica. Here’s a quick script we used to find out what files were missing:

$dir1 = “\\server1\c$\folder1”
$dir2 = “\\server2\c$\folder1”
$d1 = get-childitem -path $dir1 -recurse
$d2 = get-childitem -path $dir2 -recurse
$results = @(compare-object $d1 $d2)

foreach($result in $results)


Directory: \\server1\c$\folder1\subfolder

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 2/16/2012 4:08 PM 162 ~$README.txt

This will output all of the files and directories that exist in only one location.

2 thoughts on “Compare Two Directories with Powershell

  1. Thanks Jeff. An elegant tip. I didn’t know the power of Compare-Object.
    If only interested in file names and not file attributes or dates, you might speed up the comparison on very large folders by collecting and comparing only file names (instead of the entire object). And case-sensitive comparisons are usually faster in code. Tweaks to three lines in your script should do that, as per below:
    $d1 = get-childitem -path $dir1 -name -recurse
    $d2 = get-childitem -path $dir2 -name -recurse
    $results = @(compare-object -casesensitive $d1 $d2)

  2. Pingback: PowerShell: compare 2 directory’s | Marc Valk dot Net

Leave a Reply

Your email address will not be published. Required fields are marked *