Analyzing and Removing Print Jobs

by Jan 21, 2015

Windows 8.1 or Server 2012 R2

Both Windows 8.1 and Server 2012 R2 come with a module called “PrintManagement”. It includes all cmdlets needed to manage local and remote printers.

In a previous tip we illustrated how to read print jobs. Each print job has the JobStatus property that tells you whether the PrintJob ran successfully.

All available status codes can be retrieved like this:

 
PS> Import-Module PrintManagement
 
PS> [Microsoft.PowerShell.Cmdletization.GeneratedTypes.PrintJob.JobStatus]::GetNames([Microsoft.PowerShell.Cmdletization.GeneratedTypes.PrintJob.JobStatus])
Normal
Paused
Error
Deleting
Spooling
Printing
Offline
PaperOut
Printed
Deleted
Blocked
UserIntervention
Restarted
Complete
Retained
RenderingLocally
 

Next, you could filter existing print jobs, and, for example, remove print jobs that have been completed or had errors. This will list all print jobs that had errors or have completed:

$ComputerName = $env:COMPUTERNAME

Get-Printer -ComputerName $ComputerName |  ForEach-Object { 
  Get-PrintJob -PrinterName $_.Name -ComputerName $ComputerName |
    Where-Object { $_.JobStatus -eq 'Complete' -or $_.JobStatus -eq 'Error' -or $_.JobStatus -eq 'Printed'}
 } 

To remove these print jobs, simply add Remove-PrintJob:

$ComputerName = $env:COMPUTERNAME

Get-Printer -ComputerName $ComputerName |  ForEach-Object { 
  Get-PrintJob -PrinterName $_.Name -ComputerName $ComputerName |
    Where-Object { $_.JobStatus -eq 'Complete' -or $_.JobStatus -eq 'Error' -or $_.JobStatus -eq 'Printed'}
 } |
 Remove-PrintJob -CimSession $ComputerName

Twitter This Tip! ReTweet this Tip!