diff --git a/scripts/Windows/Scripts/Context.ps1 b/scripts/Windows/Scripts/Context.ps1 index eba59b3b..d2d0648d 100644 --- a/scripts/Windows/Scripts/Context.ps1 +++ b/scripts/Windows/Scripts/Context.ps1 @@ -65,12 +65,15 @@ class Context { } [void] ProcessDefaultUserKey([System.Action[Microsoft.Win32.RegistryKey]] $action) { - $root = "HKLM:\DefaultUser"; - $regRoot = $root.Replace(":", ""); + $rootPath = "HKLM:\DefaultUser"; + $regRootPath = $rootPath.Replace(":", ""); $hivePath = "$env:SystemDrive\Users\Default\NTUSER.dat" - $null = & reg load $regRoot $hivePath; - $action.Invoke((Get-Item $root)); - reg unload $regRoot; + $null = & reg load $regRootPath $hivePath; + $root = Get-Item $rootPath; + $action.Invoke($root); + $root.Handle.Close(); + [System.GC]::Collect(); + & reg unload $regRootPath; } [void] ProcessLogonKey([System.Action[Microsoft.Win32.RegistryKey]] $action) { @@ -104,7 +107,9 @@ class Context { } [void] RegisterReboot([Microsoft.Win32.RegistryKey] $userKey) { - $null = Set-ItemProperty -Path $this.GetRunOnceKey($userKey).PSPath -Name $this.RunOnceName -Value "pwsh `"$($this.EntryPoint)`"" -Type "ExpandString"; + $runOnceKey = $this.GetRunOnceKey($userKey); + $null = Set-ItemProperty -Path $runOnceKey.PSPath -Name $this.RunOnceName -Value "pwsh `"$($this.EntryPoint)`"" -Type "ExpandString"; + $runOnceKey.Handle.Close(); } [void] RegisterNewUserReboot() {