Created
October 19, 2024 12:38
-
-
Save sjorspa/d729923edf721f79ccb02fa70135f38c to your computer and use it in GitHub Desktop.
Delete duplicate files in folder
This file contains 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
$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