Script to Purge Stuck Print Jobs

Create a Task Scheduler:
- Program to run: powershell.exe
- Argument: -ExecutionPolicy Bypass c:\scripts\clearStuckQueue.ps1
- Trigger: daily, repeat every 5 minutes

# Clear Print Queue from Stuck Jobs
#---------------------------------------------
# define "stuck" print jobs
$stuck = Get-ChildItem -path C:\Windows\System32\spool\PRINTERS | where {$_.Lastwritetime -lt (date).addminutes(-60)}
If ($stuck) {
net stop spooler /y
sleep 5;
$stuck | remove-item -Force;
Start-Service -Name spooler
}
#-----------------------------------------

#Alternative
#-------------------------------------------
Get-Printer -ComputerName localhost | get-printjob | where{$_.SubmittedTime -lt (date).addminutes(-1) } | Remove-PrintJob
#-------------------------------------------

Experimental
#-----------------------------------------
$computer = "localhost";
#Query for errors in print queue
Write-Verbose "Checking for errors in print queue on $computer."
$queue = Get-WMIObject win32_printer -computername $computer | where {$_.PrinterState -match '[28]'}
#Execute when error condition exists
If ($queue)
{
#Clear out errors
Write-Verbose "Clearing out print jobs with errors..."
$queue | % {
$_.CancelAllJobs() | Out-Null
}
}
#-----------------------------------------

Leave a Reply

Your email address will not be published. Required fields are marked *