Remove duplicates and recover original filenames on Windows

A pasthard drive meltdown, which was the one I was syncing Google Drive to, continues to strike waves as I try to recover my system, especially my photos which have changed names, went lost, and who knows what else.

One problem I encountered was that many files have been duplicated and Windows graciously gave them dup names (“file.ext” was replicated as “file (1).ext” and a linear legacy of successors). The following two powershell scripts take care of sorting that mess:

(a) Remove duplicate files

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

(b) Restore filenames when only a filename with a (1) suffix exists

get-childitem * -Recurse | foreach { rename-item $_ $_.Name.Replace(" (1).", ".") }