Skip to content

Instantly share code, notes, and snippets.

@brianpeiris
Forked from jstangroome/Get-DuplicateItems.ps1
Created October 27, 2012 07:51
Show Gist options
  • Select an option

  • Save brianpeiris/3963431 to your computer and use it in GitHub Desktop.

Select an option

Save brianpeiris/3963431 to your computer and use it in GitHub Desktop.
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 }
}
@brianpeiris
Copy link
Copy Markdown
Author

Fixed type reference and filtered out directories.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment