在Powershell中查找文件副本的脚本
我想在PowerShell中创建一个分析recursevily目录的脚本,并从第一个目录中的所有文件和所有文件中的所有文件获取所有哈希MD5。在Powershell中查找文件副本的脚本
之后,我想比较对方之间的所有散列,看看哪一个是副本,然后给出一个选项来删除这些副本或不。
目前,我有这样的:
$UserInput=Read-Host
Get-ChildItem -Path $UserInput -Recurse
$someFilePath = $UserInput
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($someFilePath)))
$hash
的主要问题是在散列部,我在调用“ReadAllBytes”得到一个错误。
我也怀疑如果创建一个数组,所以当我比较散列时,如果它们相等,则将副本放在数组中,因此删除它们“更容易”。
您认为如何? (我也不确定我是否正确使用“SomeFilePath”,MD5或哈希)。
如果在Windows 10个指定的PowerShell 5.1,我会使用Group-Object
cmdlet的使用它们的Get-FileHash
小命令,然后按哈希:
$UserInput = Read-Host
$DuplicateFiles = Get-ChildItem -Path $UserInput -Recurse -File |Group {($_|Get-FileHash).Hash} |Where Count -gt 1
foreach($FileGroup in $DuplicateFiles)
{
Write-Host "These files share hash $($FileGroup.Name)"
$FileGroup.Group.FullName |Write-Host
}
试试这个:
$fileHashes = Get-ChildItem -Path $myFilePath -Recurse -File | Get-Filehash -Algorithm MD5
$doubles = $fileHashes | Group hash | ? {$_.count -gt 1} | % {$_.Group}
foreach($item in $doubles) {
Write-Output $item
}
很棒!谢谢!有没有办法获得MD5散列(这是哈希,但十六进制) –
是的,我更新了这篇文章。 – k7s5a
只要做到这一点
Get-ChildItem -Path $UserInput -Recurse -File | Get-FileHash | Group Hash | Where Count -gt 1
简短版本:
gci -Path $UserInput -R -File | Get-FileHash | Group Hash | ? Count -gt 1
您定位的是哪个版本的PowerShell?您可以将所有文件哈希卸载到'Get-FileHash',它将支持相对路径和PS提供程序路径。 –
PS.Version:5.1.14393.1198 BuildVersion 10.0.14393.1198 –