-
-
Save brianpeiris/3963431 to your computer and use it in GitHub Desktop.
The script from my blog post at http://blog.codeassassin.com/2007/10/13/find-duplicate-files-with-powershell/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ([string] $Path = (Get-Location)) | |
function Get-MD5 ( | |
[System.IO.FileInfo] | |
$file = $(throw 'Usage: Get-MD5 [System.IO.FileInfo]') | |
) { | |
# This Get-MD5 function sourced from: | |
# http://blogs.msdn.com/powershell/archive/2006/04/25/583225.aspx | |
$stream = $null | |
$hashAlgorithm = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider | |
$stream = $file.OpenRead() | |
$hashByteArray = $hashAlgorithm.ComputeHash($stream) | |
$stream.Close() | |
## We have to be sure that we close the file stream if any exceptions are thrown. | |
trap { | |
if ($stream -ne $null) { $stream.Close() } | |
break | |
} | |
return [string]$hashByteArray | |
} | |
$fileGroups = Get-ChildItem -Path $Path -Recurse | | |
Where-Object { -not $_.PSIsContainer } | | |
Where-Object { $_.Length -gt 0 } | | |
Group-Object Length | | |
Where-Object { $_.Count -gt 1 } | |
foreach ($fileGroup in $fileGroups) { | |
foreach ($file in $fileGroup.Group) { | |
Add-Member -MemberType NoteProperty -Name ContentHash -Value (Get-MD5 $file) -InputObject $file | |
} | |
$fileGroup.Group | | |
Group-Object ContentHash | | |
Where-Object { $_.Count -gt 1 } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fixed type reference and filtered out directories.