Finding Duplicate Files

Hash Tables are a great way to find duplicates. Simply use the Hash Table as lookup to see if the file (or element) was already added to the Hash Table. The following script would find all files and folder with same name in the current folder, the windows folder and its system32 subfolder.

function Find-Duplicates 
{
$Input | ForEach-Object {
if ($lookup.ContainsKey($_.Name.toLower()))
{
"$($_.FullName) is a duplicate in $($lookup.$($_.Name.toLower()))"
} else
{
$lookup.Add($_.Name.toLower(), $_.FullName)
}
}
}
$lookup = @{}
dir | Find-Duplicates
dir $env:windir | Find-Duplicates
dir $env:windir\system32 | Find-Duplicates

Posted Nov 17 2008, 08:00 AM by ps1
Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.