86 lines
2.7 KiB
PowerShell
86 lines
2.7 KiB
PowerShell
$flagFolder = "C:\ProgramData\PrinterHKeyClean"
|
|
$flagFile = "${flagFolder}\v2.flag"
|
|
|
|
if (!(Test-Path $flagFolder)) {
|
|
New-Item -Path $flagFolder -ItemType Directory -Force | Out-Null
|
|
}
|
|
|
|
if (Test-Path $flagFile) {
|
|
Write-Host "Script has already run. Exiting." -ForegroundColor Red
|
|
exit
|
|
}
|
|
|
|
$profileListKey = 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList'
|
|
|
|
$sids = Get-ChildItem $profileListKey
|
|
|
|
$HKUsersPrinterPaths = @(
|
|
'\Printers\Connections',
|
|
'\Printers\ConvertUserDevModesCount',
|
|
'\Software\Xerox\PrinterDriver',
|
|
'\Software\Microsoft\Windows NT\CurrentVersion\Devices'
|
|
)
|
|
|
|
$HKLMPrinterPaths = @(
|
|
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Providers',
|
|
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\V4 Connections',
|
|
'HKEY_LOCAL_MACHINE\SOFTWARE\Xerox\PrinterDriver'
|
|
)
|
|
|
|
Write-Host "Stopping spooler" -ForegroundColor Yellow
|
|
net stop spooler
|
|
|
|
foreach ($HKLMPrinterPaths in $HKLMPrinterPaths) {
|
|
try {
|
|
Remove-Item -Path "Registry::${HKLMPrinterPaths}" -Recurse -Force -ErrorAction Stop
|
|
Write-Host "Removed local machine key ${HKLMPrinterPaths}" -ForegroundColor Green
|
|
} catch {
|
|
Write-Host "Error: $($_.Exception.Message)"
|
|
}
|
|
}
|
|
|
|
foreach ($sidItem in $sids) {
|
|
$sid = $sidItem.PSChildName
|
|
$profilePath = (Get-ItemProperty -Path $sid.PSPath -Name ProfileImagePath).ProfileImagePath
|
|
|
|
foreach ($HKUsersPrinterPath in $HKUsersPrinterPaths) {
|
|
try {
|
|
Remove-Item -Path "Registry::HKEY_USERS\${sid}${HKUsersPrinterPath}" -Recurse -Force -ErrorAction Stop
|
|
Write-Host "Removed user profile key HKEY_USERS\${sid}${HKUsersPrinterPath}" -ForegroundColor Green
|
|
} catch {
|
|
Write-Host "Error: $($_.Exception.Message)"
|
|
}
|
|
}
|
|
}
|
|
|
|
try {
|
|
Remove-Item -Path "C:\Windows\System32\spool\PRINTERS" -Recurse -Force
|
|
Write-Host "Removed PRINTERS from spooler" -ForegroundColor Green
|
|
} catch {
|
|
Write-Host "Error Removing PRINTERS from spooler" -ForegroundColor Red
|
|
Write-Host "Error: $($_.Exception.Message)"
|
|
}
|
|
|
|
try {
|
|
Remove-Item -Path "C:\Windows\System32\spool\V4Dirs" -Recurse -Force
|
|
Write-Host "Removed V4Dirs from spooler" -ForegroundColor Green
|
|
} catch {
|
|
Write-Host "Error Removing V4Dirs from spooler" -ForegroundColor Red
|
|
Write-Host "Error: $($_.Exception.Message)"
|
|
}
|
|
|
|
Write-Host "Starting spooler" -ForegroundColor Yellow
|
|
net start spooler
|
|
|
|
|
|
Write-Host "Running gpupdate" -ForegroundColor Yellow
|
|
gpupdate /force
|
|
|
|
try {
|
|
New-Item -Path $flagFile -ItemType File -Force
|
|
Write-Host "created flag at ${flagFile}" -ForegroundColor Green
|
|
} catch {
|
|
Write-Host "Could not create flag at ${flagFile}" -ForegroundColor Red
|
|
Write-Host "Error: $($_.Exception.Message)"
|
|
}
|