Windows – Delete old windows print jobs

printerscriptingwindows

On our primary windows 2003 print server we share about 500 printers. We typically have about 50 stuck print jobs. Although far from the end of the world it bugs the hell out of me and I tend to end up spending 30 odd mins a week searching for and deleting stuck jobs.

What I would really like is a script to delete all print jobs on all printers older than 24 hours.

Coming from a UNIX background I find windows scripting pretty confusing. Any advice on how I can get started with this task would be appreciated. Any complete solutions would be amazing. 😉

Cheers,

Mat.

Best Answer

This should be fairly simple to do in Powershell. You will find powershell a little more to your liking coming from *nix. You will however be working with WMI, which is truely a blessing and a curse.

Some example code that does what you want (NOT TESTED):

$strComputer = "."

$PrintJobs = get-wmiobject -class "Win32_PrintJob" -namespace "root\CIMV2" -computername $strComputer | Where-Object { $_.StartTime -lt $($(Get-Date).addDays(-1)) }

foreach ($job in $PrintJobs) {
    Write-Host "Canceling job $($job.JobId)"
    $job.Delete
}

Basically you will just need to get all objects from WMI where the Start Time is less than now - 24 hours.