You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本开发求助:基于哈希实现双目录差异对比

刚上手PowerShell写目录对比脚本确实容易卡壳,我帮你整理了一个清晰易懂的实现方案,完全贴合你要的需求:

实现思路

核心逻辑是先按相对路径匹配文件(避免绝对路径不同导致的匹配错误),再分三类处理:

  • 仅存在于其中一个目录的文件:通过对比相对路径集合筛选
  • 存在于两个目录但哈希不同的文件:先找出共同路径,再逐个计算哈希值对比
  • 完全匹配的文件直接忽略,不输出
完整可运行脚本
# --------------------------
# 配置部分:替换为你的实际目录路径
# --------------------------
$dir1 = "C:\Your\First\Directory"
$dir2 = "C:\Your\Second\Directory"

# --------------------------
# 1. 获取两个目录的文件列表(含相对路径)
# --------------------------
# 递归获取所有文件,并生成相对于根目录的路径
$filesInDir1 = Get-ChildItem -Path $dir1 -File -Recurse | 
    Select-Object FullName, 
                  @{Name='RelativePath'; Expression={$_.FullName.Substring($dir1.Length)}}

$filesInDir2 = Get-ChildItem -Path $dir2 -File -Recurse | 
    Select-Object FullName, 
                  @{Name='RelativePath'; Expression={$_.FullName.Substring($dir2.Length)}}

# --------------------------
# 2. 筛选仅存在于单个目录的文件
# --------------------------
$onlyInDir1 = $filesInDir1 | Where-Object { $filesInDir2.RelativePath -notcontains $_.RelativePath }
$onlyInDir2 = $filesInDir2 | Where-Object { $filesInDir1.RelativePath -notcontains $_.RelativePath }

# --------------------------
# 3. 筛选存在于两个目录但哈希不同的文件
# --------------------------
$commonFilePaths = $filesInDir1.RelativePath | Where-Object { $filesInDir2.RelativePath -contains $_ }
$contentMismatchedFiles = @()

foreach ($relativePath in $commonFilePaths) {
    # 获取两个目录中对应文件的绝对路径
    $filePath1 = ($filesInDir1 | Where-Object { $_.RelativePath -eq $relativePath }).FullName
    $filePath2 = ($filesInDir2 | Where-Object { $_.RelativePath -eq $relativePath }).FullName

    # 计算文件哈希(默认用SHA256,可替换为MD5/SHA1等)
    $hash1 = (Get-FileHash -Path $filePath1).Hash
    $hash2 = (Get-FileHash -Path $filePath2).Hash

    # 哈希不同则加入结果集合
    if ($hash1 -ne $hash2) {
        $contentMismatchedFiles += [PSCustomObject]@{
            RelativePath = $relativePath
            Directory1_File = $filePath1
            Directory2_File = $filePath2
            Directory1_Hash = $hash1
            Directory2_Hash = $hash2
        }
    }
}

# --------------------------
# 4. 格式化输出结果
# --------------------------
Write-Host "`n=== 仅存在于 $dir1 的文件 ===" -ForegroundColor Cyan
$onlyInDir1 | Select-Object FullName | Format-Table -AutoSize

Write-Host "`n=== 仅存在于 $dir2 的文件 ===" -ForegroundColor Cyan
$onlyInDir2 | Select-Object FullName | Format-Table -AutoSize

Write-Host "`n=== 存在于两个目录但内容不同的文件 ===" -ForegroundColor Cyan
$contentMismatchedFiles | Format-Table -AutoSize
关键部分解释
  • 相对路径计算:用$_.FullName.Substring($dir1.Length)去掉根目录的绝对路径,只保留文件在目录内的相对位置,这样才能正确匹配两个目录中结构相同的文件
  • 哈希对比:Get-FileHash是PowerShell内置的文件哈希计算命令,默认用SHA256算法(安全性更高,也可以通过-Algorithm参数指定MD5、SHA1等)
  • 结构化结果:用[PSCustomObject]创建自定义对象,让内容不同的文件结果更清晰,包含路径、哈希等关键信息
新手使用提示
  1. 先把脚本中的$dir1和$dir2替换成你要对比的实际目录路径
  2. 如果不需要对比子目录,去掉Get-ChildItem后的-Recurse参数即可
  3. 测试时建议先用小目录试手,避免大目录计算哈希耗时过长

内容的提问来源于stack exchange,提问作者Jeremiah Williams

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:09:05