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:
2026-05-24 11:05:01 +02:00
parent d7d16850fc
commit f47359440f
+2
View File
@@ -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) {