Skip to content

Instantly share code, notes, and snippets.

@sayedihashimi
Last active November 19, 2016 06:12
Show Gist options
  • Save sayedihashimi/0549759af3db175e5a40 to your computer and use it in GitHub Desktop.
Save sayedihashimi/0549759af3db175e5a40 to your computer and use it in GitHub Desktop.
This will find duplicate files from one folder to another.
[cmdletbinding()]
param(
[string]$sourceFolder = $pwd
)
function Find-DuplicateFiles{
[cmdletbinding()]
param(
[string]$sourceFolder = $pwd
)
process{
$allFiles = ((Get-ChildItem -Path $sourceFolder -Recurse -File).FullName)
[hashtable]$filehashmap = @{}
foreach($file in $allFiles){
if( ([string]::IsNullOrWhiteSpace($file)) -or (-not (test-path $file)) ){
continue
}
$fhash = Get-FileHash -LiteralPath $file
if(-not ($filehashmap.ContainsKey($fhash.Hash))){
$filehashmap[$($fhash.Hash)] = @()
}
$filehashmap[$($fhash.Hash)] += $file
}
$filehashmap.Keys| %{
$current = $_
if($filehashmap[$current].Count -gt 1){
# $filehashmap[$current]
'-----' | Write-Output
New-Object -TypeName psobject -Property @{
Hash = $current
Files = $filehashmap[$current]
}
}
}
}
}
Find-DuplicateFiles -sourceFolder $sourceFolder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment