How Can I Purge All the Print Jobs on a Windows 2000 Print Server?
Hey, Scripting Guy! How can I purge all the print jobs on a Windows 2000 print server?
Hey, KC. This is one of those questions we hate, not because we don’t have an answer for it – we do – but because we have to temporarily abandon one of the core tenets of the Scripting Guy Way of Life. That tenet – just a second while we blow the dust off it- is this: ADSI should be used for managing Active Directory and other directory services (including local computer accounts); WMI should be used for everything else.
Ok, so maybe it’s not the 10 Commandments or the Golden Rule. But hey, we’re scripting guys, not philosophy guys.
Although ours is a simple way of life, most of the time this tenet works just fine. For example, suppose you’re using Windows XP or Windows Server 2003 as a print server. Want to delete all the print jobs running on that computer? All you do is use a WMI script, just like we told you to:
strComputer = “.” Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)
Set colInstalledPrinters = objWMIService.ExecQuery _ (“Select * from Win32_Printer”)
For Each objPrinter in colInstalledPrinters objPrinter.CancelAllJobs() Next
But what if you’re using a Windows 2000 computer as a print server? Well, in that case we have a problem: the CancelAllJobs() method – used to delete print jobs from a printer on Windows XP and Windows Server 2003 – doesn’t exist on Windows 2000. Not only that, but there’s no comparable method to be found anywhere in the WMI repository. The cold, hard fact of the matter: you can’t use WMI to delete all the print jobs on a Windows 2000 computer.
Now, people of good moral fiber would simply stop at this point; after all, they would never compromise their principles just to delete a bunch of print jobs on a print server. We Scripting Guys have no such qualms, however; therefore, here’s an ADSI script that can delete all the print jobs on a Windows 2000 computer:
Set objComputer = GetObject(“WinNT://atl-ps-01”) objComputer.Filter = Array(“printQueue”)
For Each objPrinter in objComputer objPrinter.Purge Next
As you can see, we begin by using the WinNT provider to connect to the print server atl-ps-01. (By the way, WinNT is case-sensitive; your script will fail if you try using winnt or WINNT or any other variation.) This returns a collection of all the “things” on atl-ps-01, or at least all the things ADSI knows about: user accounts, groups, services, and – hey! – printers. Because we only care about the printers we apply a Filter that limits the returned data to printQueue objects; as you might expect, printQueue is the ADSI designation for a printer object. The filter requires just one line of code:
objComputer.Filter = Array(“printQueue”)
Incidentally, the object we’re filtering on – printQueue – must be configured as an array. This code looks like it should work, but it won’t:
objComputer.Filter = “printQueue”
All that’s left now is to loop through the collection of printers and – for each printer found – delete all the print jobs, something we can do using the Purge command. Deleting all the print jobs on all the printers takes just three lines of code:
For Each objPrinter In objComputer objPrinter.Purge Next
Not too bad, huh? If you’d like more information about managing print servers on Windows 2000 you might check out the Printing chapter in the Microsoft Windows 2000 Scripting Guide. And if you’d like more information about breaking the rules, just keep reading this column; we’re bound to break a few more before we’re through.