Posted On March 31, 2019

Script to Purge Stuck Print Jobs

kimconnect 0 comments
blog.KimConnect.com >> Windows >> 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 *

Related Post

Microsoft Dynamics Sluggish CRM Records Creation – Slow to Update Views

Symptom: Event Logs with repeated entries... Log Name: ApplicationSource: MSCRMAsyncServiceDate: 11/15/2021 01:55:49 AMEvent ID: 25349Task…

RDP: Common Connection Errors

Error Message: [Window Title]Remote Desktop Connection[Content]The remote session was disconnected because there are no Remote…

Windows Update Error Code 0x80244022

Symptom: There were some problems installing updates, but we'll try again later. If you keep…