Created
January 8, 2026 14:19
-
-
Save mmodrow/9bfa1ed0d1b7a72d4cfc875e29c3ec00 to your computer and use it in GitHub Desktop.
Identify files with duplicate content and remove all but one (or perform some other action on the dupe sets)
This file contains hidden or 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
| $groupedDuplicates = Get-ChildItem -Recurse -File ` | |
| | Group-Object -Property Length ` | |
| | Where-Object { $_.Count -gt 1 } ` | |
| | ForEach-Object { $_.Group } ` | |
| | Get-FileHash ` | |
| | Group-Object -Property Hash ` | |
| | Where-Object { $_.Count -gt 1 } ` | |
| | ForEach-Object { $_.Group } ` | |
| | Group-Object -Property hash | |
| for ($i = 0; $i -lt $groupedDuplicates.Count; $i++) { | |
| #somehow define what to delete - in this case: the longest path | |
| $first, $rest = @(($groupedDuplicates[$i].Group | Sort-Object { $_.Path.length }).path) | |
| for ($j = 0; $j -lt @($rest).Count; $j++) { | |
| Write-Host ("removing: "+ @($rest)[$j]) | |
| #Remove-Item -Path @($rest)[$j] | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment