如何在PowerShell中导出registryHIVE(NTUSER.DAT)

为了解决由三星打印驱动程序引起的registry膨胀问题,我需要find一种方法来导出用户的漫游configuration文件NTUSER.DATregistryconfiguration单元,同时在PowerShell中。 加载和导出一个NTUSER.DAT Registry Hive在REGEDIT中工作,但是到目前为止我还没有find在PowerShell中这样做的方法。

我正在使用的PowerShell脚本能够加载远程registryconfiguration单元,删除密钥并卸载。 这不提供从registryconfiguration单元文件中删除空闲空间的方法。 当在REGEDIT中手动执行此操作时,我已经能够使用超过150,000KB的臃肿NTUSER.DAT文件,然后将它们作为新的NTUSER_Clean.DATregistryconfiguration单元一直下降到780KB(对于设置相对较less的用户) 。

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" 

到目前为止,我还没有find一种方法来使用reg(reg.exe)导出为registryconfiguration单元文件。 据我所知,“reg EXPORT”参数只生成.reg文件。

使用“注册导出”不是你想用来导出registryconfiguration单元。 我没有意识到,但“reg保存”选项允许您实际保存一个registryconfiguration单元文件,如您的NTUSER.DAT。

find一个关于reg.exe选项的Microsoft文章,并使用“reg save”进行testing: http : //technet.microsoft.com/zh-cn/library/cc742108.aspx

使用reg保存的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