如何在 PowerShell 中导出注册表配置单元 (NTUSER.DAT)

如何在 PowerShell 中导出注册表配置单元 (NTUSER.DAT)

为了解决由三星打印驱动程序引起的注册表膨胀问题,我需要找到一种方法来在 PowerShell 中导出用户的漫游配置文件 NTUSER.DAT 注册表配置单元。加载和导出 NTUSER.DAT 注册表配置单元在 REGEDIT 中有效,但到目前为止,我还没有找到在 PowerShell 中执行此操作的方法。

我正在使用的 PowerShell 脚本能够加载远程注册表配置单元、删除项并卸载。这不提供从注册表配置单元文件中删除可用/空白空间的方法。在 REGEDIT 中手动执行此操作时,我能够获取超过 150,000KB 的臃肿 NTUSER.DAT 文件,然后将其导出为新的 NTUSER_Clean.DAT 注册表配置单元,一直到 780KB(对于设置相对较少的用户)。

PowerShell 示例代码:

Write-Host "Attempting to load the User Roaming Profile Registry HIVE (NTUSER.DAT)."
#Write-Host $strRemoteLocation
reg load "HKU\$strKeyName" $strRemoteLocation
Write-Host $strLine

Write-Host "Attempting to clean the Registry HIVE of Samsung SSPrint Keys."
Clean_Key $strKeyName "spd__"
Clean_Key $strKeyName "spe__"
Clean_Key $strKeyName "ssp6m"
Write-Host $strLine

# Export Registry HIVE to NTUSER_Clean.DAT
Write-Host "This section would export the Registry HIVE to a new file."
Write-Host "At this point I'm not sure how to do this."
Write-Host $strLine

# Unload the Registry HIVE
Write-Host "Attempting to unload the Registry HIVE."
[gc]::collect()
start-sleep -s 3
reg unload "HKU\$strKeyName"

到目前为止,我还没有找到使用 reg (reg.exe) 导出为注册表配置单元文件的方法。据我所知,“reg EXPORT”参数仅生成 .reg 文件。

答案1

使用“reg export”并不是您想要用来导出注册表配置单元的方法。我没有意识到,但“reg save”选项允许您实际保存注册表配置单元文件,例如您的 NTUSER.DAT。

找到一篇有关 reg.exe 选项的 Microsoft 文章,并使用“reg save”进行了测试: http://technet.microsoft.com/en-us/library/cc742108.aspx

使用 reg save 的 PowerShell 代码:

Write-Host "Attempting to load the User Roaming Profile Registry HIVE (NTUSER.DAT)."
#Write-Host $strRemoteLocation
reg load "HKU\$strKeyName" "$strRemoteHiveSourcePath\NTUSER.DAT"
Write-Host $strLine

Write-Host "Attempting to clean the Registry HIVE of Samsung SSPrint Keys."
Clean_Key $strKeyName "spd__"
Clean_Key $strKeyName "spe__"
Clean_Key $strKeyName "ssp6m"
Write-Host $strLine

# Export Registry HIVE to NTUSER_Clean.DAT
Write-Host "Attempt to save a new version of the Registry Hive."
reg save "HKU\$strKeyName" "$strRemoteHiveSourcePath\NTUSER_Clean.DAT"
Write-Host $strLine

# Unload the Registry HIVE
Write-Host "Attempting to unload the Registry HIVE."
[gc]::collect()
start-sleep -s 3
reg unload "HKU\$strKeyName"
Write-Host $strLine

# Verify that the NTUSER_Clean.DAT is found.
# If found rename NTUSER.DAT to NTUSER_OLD.DAT and then rename NTUSER_Clean.DAT to NTUSER.DAT
# Clean up NTUSER_OLD.DAT once verified that the new NTUSER.DAT is in place.
if (Test-Path "$strRemoteHiveSourcePath\NTUSER_Clean.DAT") {
    Write-Host "The Exported Registry Hive (NTUSER_Clean.DAT) was found."
    Write-Host $strLine

    Write-Host "Renaming the compacted NTUSER.DAT file to NTUSER_OLD.DAT."
    Rename-Item "$strRemoteHiveSourcePath\NTUSER.DAT" "NTUSER_OLD.DAT"

    Write-Host "Renaming the compacted NTUSER_Clean.DAT file to NTUSER.DAT."
    Rename-Item "$strRemoteHiveSourcePath\NTUSER_Clean.DAT" "NTUSER.DAT"

    # Verify we actually have a NTUSER.DAT file before removing the OLD version.
    if (Test-Path "$strRemoteHiveSourcePath\NTUSER.DAT") {
        Write-Host "Deleting the original NTUSER_OLD.DAT"
        Remove-Item "$strRemoteHiveSourcePath\NTUSER_OLD.DAT"
    }

}else {
   Write-Host "The Exported Registry Hive was NOT found."
   Write-Host "The NTUSER.DAT was NOT compacted."
} 
Write-Host $strLine

相关内容