Thursday 12 April 2012

Comparing directory contents using PowerShell

In transferring files to and from Windows Server 8 storage spaces, I found myself needing to compare source and target directories to determine if any had not copied correctly.

I could have used something like WinDiff but having been using Linux for a while, thought there should be a command line way to do it :-)

Here's what I found to work;
$d1 = get-childitem -name -path F:/from/directory -recurse
$d2 = get-childitem -name -path T:/target/directory -recurse
compare-object $d1 $d2

1 comment:

Anonymous said...

I used tis to check after Robocopy copying data from one location to another:

$a = Compare-Object $(dir -Recurse $Path1 | where { $_.PsIsContainer -eq $false } | select Name,DirectoryName,@{Name="SHA512 Hash"; Expression={get-hash $_.FullName -algorithm "sha512" }}) $(dir -Recurse $Path2 | where { $_.PsIsContainer -eq $false } | select Name,DirectoryName, {Name="SHA512 Hash"; Expression={get-hash $_.FullName -algorithm "sha512" }}) -property @("Name","SHA512 Hash")