Created
May 14, 2026 03:15
-
-
Save MacsInSpace/ba88b251b4c796e1325c8af7fb268ed4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # PrintQueue-Cleanup.ps1 | |
| # Scheduled task to run at ~midnight for stuck/errored print jobs | |
| # Requires admin rights to run due to service operations | |
| # Check for admin privileges | |
| if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { | |
| Write-Error "This script requires Administrator privileges. Please restart as Administrator." | |
| exit 1 | |
| } | |
| # Setup logging | |
| $logPath = "$env:TEMP\PrinterCleanup_$(Get-Date -Format 'yyyyMMdd_HHmmss').log" | |
| Start-Transcript -Path $logPath | |
| Write-Host "=================================================================" -ForegroundColor Cyan | |
| Write-Host "Print Job Cleanup Script - $(Get-Date)" -ForegroundColor Cyan | |
| Write-Host "=================================================================" -ForegroundColor Cyan | |
| # Approach 1: Standard removal via PowerShell cmdlets | |
| Write-Host "PHASE 1: Removing print jobs via PowerShell cmdlets..." -ForegroundColor Yellow | |
| try { | |
| $printers = Get-Printer | |
| $jobCount = 0 | |
| foreach ($printer in $printers) { | |
| Write-Host "Processing printer: $($printer.Name)" -ForegroundColor Cyan | |
| try { | |
| $printjobs = Get-PrintJob -PrinterObject $printer -ErrorAction SilentlyContinue | |
| foreach ($printjob in $printjobs) { | |
| try { | |
| Remove-PrintJob -InputObject $printjob -ErrorAction SilentlyContinue | |
| Write-Host " Removed job #$($printjob.JobId) ($($printjob.DocumentName))" -ForegroundColor Green | |
| $jobCount++ | |
| } | |
| catch { | |
| Write-Host " Failed to remove job #$($printjob.JobId): $_" -ForegroundColor Red | |
| } | |
| } | |
| } | |
| catch { | |
| Write-Host " Error processing printer $($printer.Name): $_" -ForegroundColor Red | |
| } | |
| } | |
| Write-Host "Total jobs removed in Phase 1: $jobCount" -ForegroundColor Cyan | |
| } | |
| catch { | |
| Write-Host "Error in Phase 1: $_" -ForegroundColor Red | |
| } | |
| Write-Host "=================================================================" -ForegroundColor Cyan | |
| # Approach 2: Aggressive cleanup with service restarts | |
| Write-Host "PHASE 2: Aggressive cleanup including service restarts..." -ForegroundColor Yellow | |
| # Check for PaperCut installation | |
| $paperCutInstalled = $null -ne (Get-Service -DisplayName "*PaperCut*" -ErrorAction SilentlyContinue) | |
| $lpdInstalled = $null -ne (Get-Service -DisplayName "LPDSVC" -ErrorAction SilentlyContinue) | |
| try { | |
| # Stop services | |
| if ($paperCutInstalled) { | |
| Write-Host "1. Stopping PaperCut Service..." -ForegroundColor Green | |
| Get-Service -DisplayName "*PaperCut*" | ForEach-Object { Stop-Service $_.Name -Force -ErrorAction SilentlyContinue } | |
| } else { | |
| Write-Host "1. PaperCut service not installed, skipping..." -ForegroundColor Yellow | |
| } | |
| if ($lpdInstalled) { | |
| Write-Host "1a. Stopping LPD Service..." -ForegroundColor Green | |
| Get-Service -DisplayName "LPDSVC" | ForEach-Object { Stop-Service $_.Name -Force -ErrorAction SilentlyContinue } | |
| } else { | |
| Write-Host "1a. LPD service not installed, skipping..." -ForegroundColor Yellow | |
| } | |
| Write-Host "1b. Stopping print isolation host process..." -ForegroundColor Green | |
| Stop-Process -Name "printisolationhost" -Force -ErrorAction SilentlyContinue | |
| Write-Host "2. Stopping Spooler Service..." -ForegroundColor Green | |
| Stop-Service Spooler -Force | |
| # Clear spool files | |
| $spoolPath = "$env:windir\system32\spool\PRINTERS" | |
| Write-Host "3. Clearing content in $spoolPath" -ForegroundColor Green | |
| $fileCount = (Get-ChildItem -Path $spoolPath -ErrorAction SilentlyContinue).Count | |
| Remove-Item -Path "$spoolPath\*.*" -Force -ErrorAction SilentlyContinue | |
| Write-Host " Removed $fileCount files from spool directory" -ForegroundColor Cyan | |
| # Restart services | |
| Write-Host "4. Starting Spooler Service..." -ForegroundColor Green | |
| Start-Service Spooler | |
| Start-Sleep -Seconds 5 # Give spooler time to initialize | |
| if ($lpdInstalled) { | |
| Write-Host "4a. Starting LPD Service..." -ForegroundColor Green | |
| Get-Service -DisplayName "LPDSVC" | ForEach-Object { Start-Service $_.Name -ErrorAction SilentlyContinue } | |
| } | |
| if ($paperCutInstalled) { | |
| Write-Host "5. Starting PaperCut Service..." -ForegroundColor Green | |
| Get-Service -DisplayName "*PaperCut*" | ForEach-Object { Start-Service $_.Name -ErrorAction SilentlyContinue } | |
| } | |
| Write-Host "Print queue cleanup completed successfully" -ForegroundColor Green | |
| } | |
| catch { | |
| Write-Host "Error in Phase 2: $_" -ForegroundColor Red | |
| } | |
| Write-Host "=================================================================" -ForegroundColor Cyan | |
| # Check service status at end | |
| Write-Host "Final Service Status:" -ForegroundColor Cyan | |
| Write-Host "Spooler: $((Get-Service Spooler).Status)" -ForegroundColor Cyan | |
| if ($paperCutInstalled) { | |
| Get-Service -DisplayName "*PaperCut*" | ForEach-Object { | |
| Write-Host "$($_.DisplayName): $($_.Status)" -ForegroundColor Cyan | |
| } | |
| } | |
| if ($lpdInstalled) { | |
| Get-Service -DisplayName "LPDSVC" | ForEach-Object { | |
| Write-Host "$($_.DisplayName): $($_.Status)" -ForegroundColor Cyan | |
| } | |
| } | |
| Stop-Transcript |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment