Skip to content

Instantly share code, notes, and snippets.

@mmodrow
Created January 8, 2026 14:19
Show Gist options
  • Select an option

  • Save mmodrow/9bfa1ed0d1b7a72d4cfc875e29c3ec00 to your computer and use it in GitHub Desktop.

Select an option

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)
$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