PowerShell: Cancel all print jobs

Here’s a quick PowerShell script I put together to delete all print jobs from our Windows Server 2008 R2 print server. It’s run overnight as a scheduled task. We do this because sometimes our print accounting software doesn’t clear out old jobs if users haven’t released them at the printer.

$printers = Get-WmiObject Win32_Printer

if ($printers.Count -eq 0) {
    Write-Host -ForegroundColor red "No Printers Found"
    exit
}

foreach ($printer in $printers) {

    if ($printer.Local -and $printer.Shared)
    {
        Write-Host -ForegroundColor Green "Cancelling all jobs on: $($printer.Name)"
        $printer.CancelAllJobs()

    }
}

It will only clear print jobs on local printers that are shared out. There’s some more info about the Win32_Printer class and the CancelAllJobs method on MSDN.

Note that I haven’t included any error-checking in this script at all, so run it manually first to see if it will work. As always, run at your own risk.

To schedule it, simply create a new scheduled task:

    1. Run with highest privileges
    2. Run whether user is logged on or not
    3. Do not store password
    4. Action: Start a program: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
    5. Arguments: -File “C:\path-to-script-file\name-of-script-file.ps1″

clearqueues-generaltab

clearqueues-action

This entry was posted in PowerShell, Scripting, SysAdmin and tagged , . Bookmark the permalink.

2 Responses to PowerShell: Cancel all print jobs

  1. Your script can be more efficient by using a WMI filter. Now, you are getting every printer and later going through to take an action on printers that are local and shared. The best practice is to filter as early in your expression as you can. Here’s a one line alternative.

    get-wmiobject win32_printer -filter “Local=’True’AND shared=’True’” | foreach {
    Write-Host “Cancelling all jobs on $($_.Name)” -ForegroundColor Green
    $_.CancelAllJobs()
    }

    The tricky thing is that WMI filters use the legacy operators, not PowerShell operators.

  2. DanielNo Gravatar says:

    Excellent. Goes to show why you’re an MVP… ;)

    This is my second attempt at a PS script that does anything useful. I’m still getting my head around the way things operate in PowerShell. All of the different operators, the way to refer to objects inside a loop, etc.

    I’ll certainly use the WMI filters if I do any similar scripting in the future. For now though, I’m happy to leave it as-is. I don’t need to worry too much about the efficiency of the script. It would be another story if I was running it on a server with 1000 printers.

    Thanks for leaving feedback.

    Daniel

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>