PowerShell Clear Stuck Print Jobs

  • Welcome to ITBible, we're your #1 resource for enterprise or homelab IT problems (or just a place to show off your stuff).
Just a way to clear print jobs that may be stuck "printing" or if they are in error status.
PowerShell:
$PrintJobs = get-wmiobject -class "Win32_PrintJob" -namespace "root\CIMV2" -computername "."

foreach ($job in $PrintJobs) {
    $pos = ($job.Name).IndexOf(",")
    $printerName = ($job.Name).Substring(0, $pos)
    if($job.JobStatus -like "Error | Printing") {
        #Write-Host "Canceling job $($job.JobId)"
        Remove-PrintJob -ComputerName $env:COMPUTERNAME -ID $job.JobId -PrinterName $printerName
    }
}
 
Updated a bit, allows for an array of computers, helpful if you need to also run the script on a print server.

Code:
<# 
    .NOTES
    ===========================================================================
     Created on:       12/9/2022 11:38 PM
     Created by:       WizardTux
     Organization:     IT Bible (itbible.org)
     Filename: ClearStuckPrintJobs.ps1       
    ===========================================================================
    .DESCRIPTION
        Clears print jobs on an array of computers
#>

$Computers = @(
    "." 
)

foreach ($Computer in $Computers)
{
    $name = $Computer
    if ($Computer -eq ".") { $name = "Local Machine" }
    Write-Host "Checking Print Jobs on: $($name)"
    $PrintJobs = Get-WmiObject -Class "Win32_PrintJob" -Namespace "root\CIMV2" -ComputerName $Computer
    foreach ($job in $PrintJobs)
    {
        $pos = ($job.Name).IndexOf(",")
        $printerName = ($job.Name).Substring(0, $pos)
        if ($job.JobStatus -like "Error | Printing")
        {
            Remove-PrintJob -ComputerName $env:COMPUTERNAME -ID $job.JobId -PrinterName $printerName
        }
    }
}

Any future updates to this script will be on our GitHub.