Last active
November 19, 2016 06:12
-
-
Save sayedihashimi/0549759af3db175e5a40 to your computer and use it in GitHub Desktop.
This will find duplicate files from one folder to another.
This file contains 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
[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