{"id":68243,"date":"2006-01-05T15:58:00","date_gmt":"2006-01-05T15:58:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/01\/05\/how-can-i-purge-a-print-queue-on-a-print-server\/"},"modified":"2006-01-05T15:58:00","modified_gmt":"2006-01-05T15:58:00","slug":"how-can-i-purge-a-print-queue-on-a-print-server","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-purge-a-print-queue-on-a-print-server\/","title":{"rendered":"How Can I Purge a Print Queue on a Print Server?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I purge a print queue on a print server? I need to do this on both Windows 2000 and Windows Server 2003.<BR><BR>&#8212; KD<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, KD. And hey: no fair! Lots of times we take the easy way out in this column: we provide an answer that works only on Windows XP or Windows Server 2003, and then say, \u201cIf there\u2019s enough interest we\u2019ll see if we can find a way to do this on Windows 2000 as well.\u201d If there\u2019s not enough interest, well, that just means we didn\u2019t have to work very hard on that particular column.<\/P>\n<P>But you\u2019re cheating: you specifically mentioned <I>both<\/I> Windows 2000 and Windows Server 2003. That makes it a bit harder for us to get away with the old \u201cIf there\u2019s enough interest\u201d ploy. Pretty clever, KD. Pretty clever.<\/P>\n<P>But one thing about the Scripting Guys: we always respond to a challenge. (We don\u2019t always respond <I>well<\/I> to a challenge, but we do respond.) You say you need to purge a print queue on Windows 2000. Okey-doke; here\u2019s an ADSI script that does just that:<\/P><PRE class=\"codeSample\">Set objPrinter = GetObject(&#8220;WinNT:\/\/atl-ps-01\/ArtDepartmentPrinter&#8221;)\nobjPrinter.Purge\n<\/PRE>\n<P>No, we\u2019re not taking shortcuts here: that <I>is<\/I> the entire script. In the first line, we bind to the printer ArtDepartmentPrinter on the print server atl-ps-01. And in the second line we call the <B>Purge<\/B> method to delete all the print jobs in the queue. Just that easy, just that quick.<\/P>\n<P>In other words, one down (Windows 2000) and one to go (Windows Server 2003). Now, technically, we don\u2019t have to show you another script; after all, the preceding script also works on Windows Server 2003. But just to prove that we aren\u2019t completely lazy, here\u2019s a second script, one that uses WMI but runs only on Windows Server 2003 and Windows XP. Let\u2019s look at and explain the code, then talk about why you might want to use this script to manage print queues:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ps-01&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colInstalledPrinters = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Printer Where Name = &#8216;ArtDepartmentPrinter'&#8221;)<\/p>\n<p>For Each objPrinter in colInstalledPrinters\n    objPrinter.CancelAllJobs()\nNext\n<\/PRE>\n<P>In this script we start off by connecting to the WMI service on the remote computer atl-ps-01. We then use this line of code to return a collection consisting of all the printers named ArtDepartmentPrinter (a collection that will have, at most, a single item, seeing as how printer names must be unique on a print server):<\/P><PRE class=\"codeSample\">Set colInstalledPrinters =  objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Printer Where Name = &#8216;ArtDepartmentPrinter'&#8221;)\n<\/PRE>\n<P>From there we create a For Each loop to loop through the collection, calling the <B>CancelAllJobs()<\/B> method to purge all the print jobs for each print queue in the collection.<\/P>\n<P>That\u2019s pretty straightforward, but the first script we showed you is not only shorter but it runs on Windows 2000 as well. So why would ever you want to use the WMI version? Well, if all you\u2019ll ever need to do is purge one specific print queue then we can\u2019t really think of a good reason; go ahead and use the first script. The value of the second script can be found in WMI\u2019s ability to manage printers and print queues, capabilities (found only in Windows XP and Windows Server 2003) that far exceed ADSI\u2019s ability to manage printers and print queues. <\/P>\n<P>For example, suppose you want to purge only print queues containing <I>x<\/I> number of print jobs or only print queues on your color printers. Those tasks are far easier to do using WMI than using ADSI. And what if you want to install (or uninstall) printers, printer drivers, or printer ports? You can do that with WMI; you can\u2019t do that with ADSI. Thus WMI scripting offers a lot of long-term advantages, even if, in the short-term, and for a very specific task, ADSI might seem better.<\/P>\n<P>And <I>that\u2019s<\/I> why we recommend WMI for doing most system administration tasks, leaving ADSI for chores involving users and groups and stuff like that. You can take that basic WMI script and modify it to carry out all sorts of tasks; that isn\u2019t necessarily true with the ADSI version.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I purge a print queue on a print server? I need to do this on both Windows 2000 and Windows Server 2003.&#8212; KD Hey, KD. And hey: no fair! Lots of times we take the easy way out in this column: we provide an answer that works only on Windows [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[404,3,5],"class_list":["post-68243","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-printing","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I purge a print queue on a print server? I need to do this on both Windows 2000 and Windows Server 2003.&#8212; KD Hey, KD. And hey: no fair! Lots of times we take the easy way out in this column: we provide an answer that works only on Windows [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=68243"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68243\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=68243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}