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

Remote Desktop Connection An internal error has occurred.

RDP Error Message:     Resolution: Method 1: Create and apply a self signed certificate…

Google G Suite Administrators (GAM) Useful Commands

Install Google GAM if (!(Get-Command choco.exe -ErrorAction SilentlyContinue)) {[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12Set-ExecutionPolicy Bypass -Scope Process -Force;…

Domino Email Server Conversion

Lotus Notes has been considered legacy by IBM and the general IT world. It lacks…