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

如何在远程计算机上为所有用户编辑注册表项

远程批量修改所有用户注册表项的实现方法

核心思路

每个用户的注册表配置单元存储在其用户目录下的NTUSER.DAT文件中,远程操作时不能直接通过OpenRemoteBaseKey指定用户SID访问,必须先将该文件加载到远程机器的HKLM临时键中,修改完成后再卸载,避免占用资源。

完整脚本示例

$computername = "远程计算机名" # 替换为目标远程机器名称
$targetRegPath = "Software\你的目标注册表路径" # 替换为要修改的用户注册表项路径
$valueName = "要设置的键名" # 替换为目标键名称
$valueData = "要设置的键值" # 替换为目标键值内容

# 获取所有非系统用户的SID及配置文件路径
$userProfiles = Get-WmiObject -Class Win32_UserProfile -ComputerName $computername | 
                Where-Object { $_.LocalPath -notlike "*$env:SystemRoot*" -and $_.SID }

foreach ($profile in $userProfiles) {
    $sid = $profile.SID
    $ntuserDatPath = Join-Path -Path $profile.LocalPath -ChildPath "NTUSER.DAT"
    
    try {
        # 1. 连接远程机器的HKLM注册表根键
        $regHKLM = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computername)
        
        # 2. 创建唯一临时键(用SID做后缀避免冲突)
        $tempHiveKeyName = "TempHive_$sid"
        $tempHiveKey = $regHKLM.CreateSubKey($tempHiveKeyName, $true)
        
        # 3. 加载用户配置单元到临时键
        $regLoadResult = reg load "\\$computername\HKLM\$tempHiveKeyName" "$ntuserDatPath"
        if ($LASTEXITCODE -ne 0) {
            Write-Warning "无法加载用户 $sid 的配置单元:$regLoadResult"
            $tempHiveKey.DeleteSubKeyTree("", $true)
            continue
        }
        
        # 4. 修改临时键下的目标注册表项
        $targetKey = $regHKLM.OpenSubKey("$tempHiveKeyName\$targetRegPath", $true)
        if (-not $targetKey) {
            # 路径不存在则自动创建
            $targetKey = $regHKLM.CreateSubKey("$tempHiveKeyName\$targetRegPath", $true)
        }
        $targetKey.SetValue($valueName, $valueData, [Microsoft.Win32.RegistryValueKind]::String)
        Write-Host "成功修改用户 $sid 的注册表项"
        
        # 5. 关闭注册表键并卸载配置单元
        $targetKey.Close()
        $tempHiveKey.Close()
        $regHKLM.Close()
        
        $regUnloadResult = reg unload "\\$computername\HKLM\$tempHiveKeyName"
        if ($LASTEXITCODE -ne 0) {
            Write-Warning "无法卸载用户 $sid 的配置单元:$regUnloadResult"
        }
    }
    catch {
        Write-Error "处理用户 $sid 时出错:$_"
        # 确保清理临时资源
        if ($regHKLM) { $regHKLM.Close() }
        reg unload "\\$computername\HKLM\$tempHiveKeyName" | Out-Null
    }
}

注意事项

  • 权限要求:执行脚本的账号需要拥有远程机器的管理员权限,且能访问用户目录共享(比如\\远程机器名\C$\Users\...)。
  • 用户登录状态:如果用户当前处于登录状态,其NTUSER.DAT文件会被系统锁定,无法加载修改。这种情况可提示用户注销后再操作,或使用专用工具处理已登录用户的注册表。
  • 临时键命名:用SID作为临时键后缀,避免多用户同时处理时出现命名冲突。

内容的提问来源于stack exchange,提问作者Paxton Smith

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 18:10:32