Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The script from my blog post at http://blog.stangroome.com/2007/10/13/find-duplicate-files-with-powershell/Updated to utilise PowerShell v3
#requires -version 3
[CmdletBinding()]
param (
[string]
$Path
)
function Get-MD5 {
param (
[Parameter(Mandatory)]
[string]
$Path
)
# This Get-MD5 function sourced from:
# http://blogs.msdn.com/powershell/archive/2006/04/25/583225.aspx
$HashAlgorithm = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$Stream = [System.IO.File]::OpenRead($Path)
try {
$HashByteArray = $HashAlgorithm.ComputeHash($Stream)
} finally {
$Stream.Dispose()
}
return [System.BitConverter]::ToString($HashByteArray).ToLowerInvariant() -replace '-',''
}
if (-not $Path) {
if ((Get-Location).Provider.Name -ne 'FileSystem') {
Write-Error 'Specify a file system path explicitly, or change the current location to a file system path.'
return
}
$Path = (Get-Location).ProviderPath
}
Get-ChildItem -Path $Path -Recurse -File |
Where-Object { $_.Length -gt 0 } |
Group-Object -Property Length |
Where-Object { $_.Count -gt 1 } |
ForEach-Object {
$_.Group |
ForEach-Object {
$_ |
Add-Member -MemberType NoteProperty -Name ContentHash -Value (Get-MD5 -Path $_.FullName)
}
$_.Group |
Group-Object -Property ContentHash |
Where-Object { $_.Count -gt 1 }
}
@bardy12

This comment has been minimized.

Copy link

bardy12 commented Oct 24, 2014

For removing all your junk files on your pc use DuplicateFilesDeleter.

@Allov

This comment has been minimized.

Copy link

Allov commented May 14, 2015

Also very useful to find copy pasted code in a dev project >:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.