Forum Discussion
BernieSanders
May 15, 2025Iron Contributor
Pls recommend the best duplicate file remover for pc
Recently, I've noticed that my Windows PC has accumulated a lot of duplicate files over time. I am sure most of them are photos, documents, and even music tracks. This is taking up unnecessary space,...
MittRomney
May 15, 2025Iron Contributor
Here's a comprehensive PowerShell script to find and remove duplicate files on your Windows PC. The benefit is that you don't need to install a dedicated duplicate file remover for PC. However, you should be able to read the code and change the parameters accordingly.
# Define the folder to search (change this to your target directory)
$targetFolder = "C:\Path\To\Search"
# Get all files recursively
$allFiles = Get-ChildItem -Path $targetFolder -Recurse -File
# Group files by their hash (this identifies duplicates)
$fileGroups = $allFiles | Group-Object -Property Length | Where-Object { $_.Count -gt 1 } | ForEach-Object {
$_.Group | Group-Object -Property { (Get-FileHash $_.FullName -Algorithm MD5).Hash }
} | Where-Object { $_.Count -gt 1 }
# Display duplicates found
Write-Host "Found $($fileGroups.Count) groups of duplicate files"
# Process each group of duplicates
foreach ($group in $fileGroups) {
Write-Host "`nDuplicate Group (MD5: $($group.Name))"
$filesToKeep = $group.Group | Select-Object -First 1
$filesToRemove = $group.Group | Select-Object -Skip 1
Write-Host "Keeping: $($filesToKeep.FullName)"
foreach ($file in $filesToRemove) {
Write-Host "Removing: $($file.FullName)"
# Uncomment the next line to actually delete files
# Remove-Item -Path $file.FullName -Force
}
}
How to Use These Scripts
- Change $targetFolder to the directory you want to scan
- Run the script to see what duplicates would be removed
- Uncomment the Remove-Item line to actually delete files