Delete old windows print jobs

Solution 1:

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.

Solution 2:

This could be your strategy. Stop the spooler service, delete all old files and restart the service.

Write this script code in a text file and name it "DeleteOldQueuedFile.vbs":

Dim Fso, Directory, Modified, Files
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Directory = Fso.GetFolder("%systemroot%\system32\spool\printers")
Set Files = Directory.Files

For Each Modified in Files  
   If DateDiff("D", Modified.DateLastModified, Now) >= 1 Then Modified.Delete
Next

Write a batch file which you could schedule as a nightly job:

net stop spooler
DeleteOldQueuedFile.vbs
net start spooler