如何基于文件时长删除重复文件并保留最早原文件(支持指定路径与目录)
Fixing Your PowerShell Duplicate File Remover: Add Path Support & Directory Traversal
Great start using file hashes to detect duplicates—this is the most accurate method since it checks actual file content, not just filenames or sizes. Let's tweak your script to support a target path, traverse subdirectories recursively, and ensure only the earliest original file is kept for each set of duplicates.
Here's the revised, fully functional script:
param( [Parameter(Mandatory=$false)] [string]$TargetPath = (Get-Location).Path ) # Get all files in target path and subdirectories, calculate their SHA256 hash $files = Get-ChildItem -Path $TargetPath -File -Recurse | Select-Object FullName, CreationTime, @{Name='FileHash'; Expression={(Get-FileHash -Path $_.FullName -Algorithm SHA256).Hash}} # Group files by their hash (duplicates share the same hash) $duplicateGroups = $files | Group-Object -Property FileHash | Where-Object {$_.Count -gt 1} # Process each group: keep the earliest file, delete the rest foreach ($group in $duplicateGroups) { # Sort files by creation time (oldest first) $sortedFiles = $group.Group | Sort-Object -Property CreationTime # Keep the first (earliest) file, mark the rest for deletion $keepFile = $sortedFiles[0] $deleteFiles = $sortedFiles | Select-Object -Skip 1 Write-Host "`nKeeping original file (created $($keepFile.CreationTime)): $($keepFile.FullName)" Write-Host "Marking $($deleteFiles.Count) duplicate(s) for deletion:" foreach ($file in $deleteFiles) { Write-Host "- $($file.FullName) (created $($file.CreationTime))" # Uncomment the line below to actually delete files (test first!) # Remove-Item -Path $file.FullName -Force -Confirm:$false } } Write-Host "`nDuplicate cleanup process completed."
Key Improvements & Explanations:
- Specify a Target Path: The
paramblock lets you pass a path likeE:\media\when running the script. If no path is provided, it defaults to your current working directory. - Recursive Directory Traversal: The
-Recurseflag inGet-ChildItemensures we scan every subdirectory under the target path. - Reliable Duplicate Detection: Using
Get-FileHashwith SHA256 guarantees that only files with identical content are flagged as duplicates (avoids false positives from same-size different-content files). - Preserve the Earliest File: Each group of duplicates is sorted by
CreationTime, so the oldest file is kept, and all newer copies are marked for deletion.
How to Use:
- Save the script as
Remove-Duplicates.ps1 - Open PowerShell, navigate to the script's location
- Run it with your target path:
.\Remove-Duplicates.ps1 -TargetPath "E:\media\" - Important: Test first! The deletion line is commented out—run the script once to verify which files will be removed, then uncomment the
Remove-Itemline when you're confident.
Safety Tips:
- Always back up critical files before running duplicate deletion scripts.
- If you need to scan system-protected directories, run PowerShell as Administrator.
- You can modify the hash algorithm (e.g., MD5) if needed, but SHA256 is more secure and less prone to collisions.
内容的提问来源于stack exchange,提问作者Juan Lopez
相关产品推荐
相关产品推荐

