Based on Get-DuplicateFile in PowerShellPack I have written a simple script to find and remove duplicate files. Files are compared with a SHA1 hash value.
$a = @(get-duplicatefile *.* | foreach {$_.value} | foreach {$_.name})
$b = @(foreach ($i in 1..$a.length) {if ($i%2) {[void]$foreach.moveNext()} $a[$i] })
foreach ($i in $b) {remove-item .\$i}
If you want to search for duplicate files in multiple subdirectories:
$a = @(get-duplicatefile c:\mydirectory -recurse | foreach {$_.value} | foreach {$_.fullname})
$b = @(foreach ($i in 1..$a.length) {if ($i%2) {[void]$foreach.moveNext()} $a[$i] })
foreach ($i in $b) {remove-item $i}
Note: remember to install PowerShellPack first !
If you see this error: Remove-Item : Cannot bind argument to parameter 'Path' because it is null, just run the script again to complete the job.
Warning: it's always wise to make a backup of your important files before you start to remove files. Use this script on your own risk!
Recent Comments