Delete duplicate files in a folder using PowerShell

If you have a folder with duplicate files that you want to delete, one way to do this is by using PowerShell. This involves computing the hash of each file, comparing the files based on their hash value, and deleting the files that have identical hash values.

ls *.* -recurse | get-filehash | group -property hash | where { $_.count -gt 1 } | % { $_.group | select -skip 1 } | del

Here's what going on in detail:

ls *.* -recurse             # get all files in the current folder and all subfolders
get-filehash                # calculate the file hash for all files
group -property hash        # group them by the hash value
where { $_.count -gt 1 }    # select those groups with more than 1 item
% {                         # for all groups
    $_.group |              # output the group content, which are the files
    select -skip 1          # select all but the first file 
   }                        # (we don't want to delete all of them right?)
del                         # delete the file