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:
- Run with highest privileges
- Run whether user is logged on or not
- Do not store password
- Action: Start a program: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
- Arguments: -File “C:\path-to-script-file\name-of-script-file.ps1″