From 90a27b5a8f92eb708377918ab67fc2546494e6c2 Mon Sep 17 00:00:00 2001 From: poslop Date: Tue, 13 Jan 2026 09:30:49 -0600 Subject: [PATCH] remove printer hkey --- RemoveHKLMPrinters.ps1 | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 RemoveHKLMPrinters.ps1 diff --git a/RemoveHKLMPrinters.ps1 b/RemoveHKLMPrinters.ps1 new file mode 100644 index 0000000..6da09b6 --- /dev/null +++ b/RemoveHKLMPrinters.ps1 @@ -0,0 +1,31 @@ +$profileListKey = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList' + +$sids = Get-ChildItem $profileListKey | + Where-Object { $_.PSChildName -match '^S-1-5-21-\d+-\d+-\d+-\d+$' } + +foreach ($sidKey in $sids) { + $sid = $sidKey.PSChildName + $profilePath = (Get-ItemProperty -Path $sidKey.PSPath -Name ProfileImagePath).ProfileImagePath + + if (-not $profilePath) { continue } + + $ntUserDat = Join-Path $profilePath 'NTUSER.DAT' + if (-not (Test-Path $ntUserDat)) { continue } + + $mounted = $false + try { + if (Test-Path "Registry::HKEY_USERS\$sid") { $mounted = $true } + } catch { $mounted = $false } + + if (-not $mounted) { + $loadResult = & reg.exe load "HKU\$sid" "$ntUserDat" 2>&1 + } + + try { + Remove-Item -Path "Registry::HKEY_USERS\$sid\Printers" -Recurse -Force -ErrorAction Stop + } catch {} + + if (-not $mounted) { + $unloadResult = & reg.exe unload "HKU\$sid" 2>&1 + } +}