用 PowerShell 按哈希查找重复文件
通过 Get-FileHash 计算 SHA256 哈希并用 Group-Object 分组,找出内容完全相同的重复文件,方便清理磁盘。
在 PowerShell 中执行:Get-ChildItem 指定文件夹 -Recurse -File 后用管道传给 Get-FileHash,再用 Group-Object Hash 分组,保留数量大于 1 的组。
01命令
命令
$ $files = Get-ChildItem 'C:\Users\me\Pictures' -Recurse -File
$files | Get-FileHash -Algorithm SHA256 |
Group-Object Hash | Where-Object Count -gt 1 |
ForEach-Object { $_.Group | Select-Object Path, Hash } | Format-Table -AutoSize02作用说明
文件名和大小相同不代表内容一样,但 SHA256 哈希只有在文件内容逐字节相同时才会一致。按哈希分组后,每一组就是一份文件的所有副本,组内成员多于一个即说明存在重复文件。抽查几组确认无误后即可删除多余副本;注意有些文件本来就是相同内容(比如空的占位文件、许可协议),它们也会被列出来。
03风险
!注意: 对很大的文件夹计算哈希会占用大量磁盘和 CPU、耗时数分钟,建议只对特定文件夹运行,不要扫描整个磁盘。
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- Get-FileHash依据: Get-FileHash computes the SHA256 hash of files piped in, so identical files produce identical hashes that can be grouped.
- Get-FileHash - SS64依据: Get-FileHash accepts pipeline input of FileInfo objects and supports SHA256 as the default algorithm.
最后验证: 2026-09-18 · 分类: 文件与文件夹