Fix access denied when unloading default user registry hive
Force .NET garbage collection before reg.exe unload to release open RegistryKey handles left by PowerShell registry cmdlets. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -318,6 +318,8 @@ function Set-DefaultUserProfileDefaults {
|
|||||||
finally {
|
finally {
|
||||||
if ($HiveWasLoaded) {
|
if ($HiveWasLoaded) {
|
||||||
Write-Host "Unloading default user registry hive..." -ForegroundColor Cyan
|
Write-Host "Unloading default user registry hive..." -ForegroundColor Cyan
|
||||||
|
[GC]::Collect()
|
||||||
|
[GC]::WaitForPendingFinalizers()
|
||||||
& reg.exe unload "HKU\DefaultUser" | Out-Null
|
& reg.exe unload "HKU\DefaultUser" | Out-Null
|
||||||
|
|
||||||
if ($LASTEXITCODE -ne 0) {
|
if ($LASTEXITCODE -ne 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user