Skip to content

Instantly share code, notes, and snippets.

@nerdshark
Created August 27, 2015 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nerdshark/39e810920a201b750228 to your computer and use it in GitHub Desktop.
Save nerdshark/39e810920a201b750228 to your computer and use it in GitHub Desktop.
Powershell duplicate file search
function Get-DuplicateFiles
{
param
(
[Parameter(Mandatory=$true)]
[string] $Directory
)
$Duplicates = @{}
ls -Recurse -File $Directory |
select -ExpandProperty FullName |
% { Get-FileHash $_ -Algorithm SHA512 } |
% { $key = $_.Hash;
if ($Duplicates[$key] -eq $null) { $Duplicates[$key] = @() }
$Duplicates[$key] += $_.Path
}
$uniqueHashes = $Duplicates.Keys | ? { $Duplicates[$_].Count -lt 2 }
$uniqueHashes | % { $Duplicates.Remove($_) }
$Duplicates
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment