{"id":68223,"date":"2006-01-09T15:55:00","date_gmt":"2006-01-09T15:55:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/01\/09\/how-can-i-remove-all-the-black-and-white-printers-on-a-computer\/"},"modified":"2006-01-09T15:55:00","modified_gmt":"2006-01-09T15:55:00","slug":"how-can-i-remove-all-the-black-and-white-printers-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-remove-all-the-black-and-white-printers-on-a-computer\/","title":{"rendered":"How Can I Remove All the Black-and-White Printers on a Computer?"},"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! On a Windows XP computer, how can I remove all the black-and-white printers, leaving only color printers?<BR><BR>&#8212; JL<\/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, JL. You know, in one classic episode of the Simpsons, Bart, in the throes of a sugar high, joins the Junior Campers. The next morning, realizing what he has done, he vows to weasel out of his commitment. His mother tries to stop him, but Homer interrupts: \u201cLearning how to weasel out of things is important,\u201d he says. \u201cIt\u2019s what separates us from the animals \u2026 except the weasel, of course.\u201d<\/P>\n<P>As you might have guessed, the Scripting Guys are about to try and separate ourselves from the animals. Not that we\u2019re going to totally weasel out of answering the question; heaven forbid! It\u2019s just that we\u2019re going to stick very close to the actual wording of the question, and only explain how to do this on Windows XP and Windows Server 2003.<\/P>\n<P>Why are the Scripting Guys &#8211; of all people &#8211; suddenly sticklers for protocol? Well, as it turns out, removing just the black-and-white printers on a Windows XP or Windows 2003 computer is pretty easy. That\u2019s not the case on, say, Windows 2000. On that version of Windows you can delete printers on the local computer, but you can\u2019t (at least not easily) delete printers on remote computers. Trying to do things remotely that you can\u2019t really do remotely always leads to far too many problems for us to address in this column. Therefore, we\u2019ll just answer the question at hand &#8211; how do I do this on Windows XP? &#8211; and pretend we never even thought about how (or if) this might work on Windows 2000.<\/P>\n<TABLE id=\"E1C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. OK, OK. Let us know if you need to perform this task on Windows 2000; if there\u2019s enough interest we\u2019ll reconsider our weasel-like behavior.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>With that in mind, here\u2019s the answer to your question, JL:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colPrinters = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_PrinterConfiguration Where Color = 1&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    strName = objPrinter.Name\n    strName = Replace(strName, &#8220;\\&#8221;, &#8220;\\\\&#8221;)<\/p>\n<p>    Set colBWPrinters = objWMIService.ExecQuery _\n        (&#8220;Select * from Win32_Printer Where Name = &#8216;&#8221; &amp; strName &amp; &#8220;&#8216;&#8221;)\n    For Each objBWPrinter in colBWPrinters\n        objBWSPrinter.Delete_\n    Next\nNext\n<\/PRE>\n<P>As you can see, after all that buildup there really isn\u2019t all that much to the script. We start off by binding to the WMI service on the local computer, although &#8211; as hinted at above &#8211; we could just as easily perform this task on a remote computer. We then use this line of code to connect to the <B>Win32_PrinterConfiguration<\/B> class and retrieve a collection of all the printers on the computer that do <I>not<\/I> print color:<\/P><PRE class=\"codeSample\">Set colPrinters = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_PrinterConfiguration Where Color = 1&#8221;)\n<\/PRE>\n<P>That might sound a bit backwards, but, remember, we want to keep all the color printers. The easiest way to do that is to grab a collection of all the black-and-white printers and then delete each printer in the collection. Do that, and, when you\u2019re done, all you\u2019ll have left are color printers.<\/P>\n<P>We\u2019re willing to bet your average weasel would <I>never<\/I> have thought of a solution like that.<\/P>\n<P>Hey, we said your <I>average<\/I> weasel.<\/P>\n<P>As you probably already figured out, if the value of the <B>Color<\/B> property is 1 then the printer is a black-and-white printer. If the value is 2, then you have a color printer. Those, by the way, are the only possible values for the Color property<\/P>\n<P>After we get back our collection we then set up a For Each loop to walk through all the items in the collection. For the first item (that is, our first black-and-white printer) we use this line of code to retrieve the printer name and store it in a variable named strName:<\/P><PRE class=\"codeSample\">strName = objPrinter.Name\n<\/PRE>\n<P>That brings us to this line of code:<\/P><PRE class=\"codeSample\">strName = Replace(strName, &#8220;\\&#8221;, &#8220;\\\\&#8221;)\n<\/PRE>\n<P>What we\u2019re doing here is replacing any \\ characters in the printer name with \\\\. Why the heck do we do that? Well, if we have a network printer it\u2019s going to have a name like this: \\\\atl-ps-01\\finance_printer. That\u2019s fine, but we need to use the printer name in a WQL query. Because the \\ is a reserved character in WQL we need to \u201cescape it\u201d (double it up). In other words, in our query the printer name needs to look like this: \\\\\\\\atl-ps-01\\\\finance_printer. That\u2019s what we use the <B>Replace<\/B> function for: it replaces (escapes) each instance of the \\ with \\\\. <\/P>\n<P>Got that? Good. Now, as it turns out, the Win32_PrinterConfiguration class can tell us whether or not a printer can print in color. What it <I>can\u2019t<\/I> do, however, is delete a printer. To actually get rid of the black-and-white printers we need to connect to the <B>Win32_Printer<\/B> class, which is what we do here:<\/P><PRE class=\"codeSample\">Set colBWPrinters = objWMIService.ExecQuery _\n        (&#8220;Select * from Win32_Printer Where Name = &#8216;&#8221; &amp; strName &amp; &#8220;&#8216;&#8221;)\n<\/PRE>\n<P>Notice that we use a Where clause here, indicating that we only want to retrieve those printers where the <B>Name<\/B> is equal to the variable strName. (And, remember, we just doubled up all the \\ characters in strName.) Because printer names must be unique on a computer this ensures that we will get back a collection consisting of just one printer.<\/P>\n<P>And what do we do with that collection? That\u2019s easy. We set up a For Each loop to walk through it, something we have to do even though there\u2019s only one item in the collection. Inside <I>that<\/I> For Each loop we call the <B>Delete_<\/B> method to delete the printer:<\/P><PRE class=\"codeSample\">objBWSPrinter.Delete_\n<\/PRE>\n<TABLE id=\"EAF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Incidentally, this is why our script won\u2019t work on Windows 2000: on versions of Windows prior to Windows XP, the Win32_Printer class does not support the Delete_ method.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>That single line of code deletes our first black-and-white printer. We then loop around to our original For Each loop and repeat the process with the second printer in the collection. This continues until all the black-and-white printers have been deleted.<\/P>\n<P>And <I>that\u2019s<\/I> the kind of thing that separates the Scripting Guys from the animals \u2026 except the Australian scripting badger, of course. But other than that \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! On a Windows XP computer, how can I remove all the black-and-white printers, leaving only color printers?&#8212; JL Hey, JL. You know, in one classic episode of the Simpsons, Bart, in the throes of a sugar high, joins the Junior Campers. The next morning, realizing what he has done, he vows to [&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":[445,404,3,5],"class_list":["post-68223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-printing","tag-printing","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! On a Windows XP computer, how can I remove all the black-and-white printers, leaving only color printers?&#8212; JL Hey, JL. You know, in one classic episode of the Simpsons, Bart, in the throes of a sugar high, joins the Junior Campers. The next morning, realizing what he has done, he vows to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68223","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=68223"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68223\/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=68223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}