Skip to content

Instantly share code, notes, and snippets.

@sjorspa
Created October 19, 2024 12:38
Show Gist options
  • Save sjorspa/d729923edf721f79ccb02fa70135f38c to your computer and use it in GitHub Desktop.
Save sjorspa/d729923edf721f79ccb02fa70135f38c to your computer and use it in GitHub Desktop.
Delete duplicate files in folder
$list = New-Object Collections.Generic.List[String]
$results = Get-ChildItem -Path C:\backup2 -Recurse
foreach ($item in $results) {
if ($item.Attributes -ne 'Directory') {
$hash = Get-FileHash -LiteralPath $item.FullName
if ($hash.Hash -ne '') {
if ($list.Contains($hash.Hash)) {
try {
Remove-Item -LiteralPath $item.FullName -force -Recurse
}
catch {
$error[0]
}
}
else {
$list.Add($hash.Hash)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment