Files
Windows-Admin-Scripts/RemoveHKLMPrinters.ps1
2026-01-13 09:30:49 -06:00

32 lines
952 B
PowerShell

$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
}
}