{"id":63663,"date":"2007-11-02T03:23:00","date_gmt":"2007-11-02T03:23:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/11\/02\/hey-scripting-guy-how-can-i-remove-all-the-network-printers-on-a-computer\/"},"modified":"2007-11-02T03:23:00","modified_gmt":"2007-11-02T03:23:00","slug":"hey-scripting-guy-how-can-i-remove-all-the-network-printers-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-remove-all-the-network-printers-on-a-computer\/","title":{"rendered":"Hey, Scripting Guy! How Can I Remove All the Network Printers on a Computer?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>Hey, Scripting Guy! Is it possible to delete or remove all network printers from a workstation with a VBScript script, similar to the way you removed all the network drives on a computer?<BR><BR>&#8212; WDC<\/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, WDC. If you\u2019ve been following our daily <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/teched07\/itpreview.mspx\"><B>TechEd preview report<\/B><\/A>, then you know that the Scripting Guys have been busy preparing for their upcoming trip to Barcelona and <A href=\"http:\/\/www.mseventseurope.com\/teched\/07\/itforum\/Content\/Pages\/Default.aspx\" target=\"_blank\"><B>TechEd IT Forum<\/B><\/A>. The preview report gives you an inside look at what the Scripting Guys are doing to get ready for the trip; before we answer today\u2019s question, however, we thought we\u2019d give you an exclusive <I>super-duper <\/I>inside look at what we\u2019re doing to get ready for the trip.<\/P>\n<P>For example, here\u2019s the transcript of the Scripting Guys\u2019 planning session that took place earlier today:<\/P>\n<P><B>Jean Ross<\/B>: We have <I>way<\/I> too much to do.<\/P>\n<P><B>The Scripting Guy Who Writes This Column<\/B>: That\u2019s true, we <I>do<\/I> have way too much to do.<\/P>\n<P><B>Jean Ross<\/B>: I don\u2019t see how we can possibly get everything done in time.<\/P>\n<P><B>The Scripting Guy Who Writes This Column<\/B>: Me either, I \u2013 wait, <I>I<\/I> know: suppose that in addition to everything else we\u2019re doing, we added three more things to the pile? That would solve all our problems, right?<\/P>\n<P><B>Jean Ross<\/B>: &#8212;<\/P>\n<P>Well, uh, never mind what Scripting Guy Jean Ross said. Poor thing; she\u2019s been under a lot of stress lately. <\/P>\n<P>Although, admittedly, most of that stress <I>does<\/I> come from having to work with Scripting Guy who writes this column.<\/P>\n<P>In other words, things are going just great. Assuming that Jean doesn\u2019t explode between now and November 8<SUP>th<\/SUP>, the Scripting Guys will hop onto a plane that morning and head for Spain. <\/P>\n<TABLE id=\"EUE\" 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>. You know, you\u2019d be surprised how many people have said that very same thing: poor Spain. Wonder what you all mean by that?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>At any rate, things are going <I>so<\/I> well that we have plenty of time to sit down and chat about a script that can remove all the network printers from a workstation. In the interests of fair disclosure we should note that, back in December of 2005 (when Scripting Guy Greg Stemp was just a sophomore in high school), we showed everyone a similar script, one that could <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/dec05\/hey1201.mspx\"><B>remove all the local printers<\/B><\/A> from a computer. Depending on how you want to look at, that means that we\u2019re cheating a little here; after all, we only have to make one minor change to that script and it will remove all the network printers instead. <\/P>\n<P>You know, now that we think about it, that doesn\u2019t sound like cheating a little, that sounds like cheating <I>a lot<\/I>.<\/P>\n<P>On the other hand, we also noted in that December 2005 column that the script in question would not work on Windows 2000; instead, it would only run on versions of Windows that came <I>after<\/I> Windows 2000. We also said, \u201cWe\u2019ll see what we can do about addressing this issue on Windows 2000 computers. We won\u2019t make any promises, but we\u2019ll look into it.\u201d And now, less than 2 years later, we\u2019ve actually gone ahead and looked into it. Based on that exhaustive research, we\u2019ll show you how to delete network printers on post-Windows 2000 computers first, and then show you how to perform this very same task on a Windows 2000 machine.<\/P>\n<P>And then at that point we\u2019ll have done everything we ever promised to do.<\/P>\n<P>More or less \u2026.<\/P>\n<P>Here\u2019s the post-Windows 2000 script:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set 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 Network = True&#8221;)<\/p>\n<p>For Each objPrinter in colInstalledPrinters\n    objPrinter.Delete_\nNext\n<\/PRE>\n<P>As you can see, there\u2019s really not much to this script. We start out by connecting to the WMI service on the local computer. As is invariably the case, however, we could also use this script to remove printers from a remote machine. How? Simply by assigning the name of that remote computer to the variable strComputer, like so:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ws-001&#8221;\n<\/PRE>\n<P>After we connect to the WMI service we then issue the following query in order to return a collection of all the network printers installed on the computer:<\/P><PRE class=\"codeSample\">Set colInstalledPrinters = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Printer Where Network = True&#8221;)\n<\/PRE>\n<P>The secret here \u2013 not that it\u2019s much of a secret, mind you \u2013 is that we included a Where clause that limits the returned data to printers where the <B>Network<\/B> property is True; needless to say, that property will be True only if we\u2019re dealing with a network printer. This, by the way, is one reason the script won\u2019t work on a Windows 2000 machine: the <B>Win32_Printer<\/B> class in Windows 2000 doesn\u2019t include the Network property. (That property was introduced in Windows XP). And no, there is <I>zero<\/I> chance that the Windows 2000 version of the Win32_Printer class will be updated to include this property.<\/P>\n<P>But don\u2019t worry, Windows 2000 users: like we said, we\u2019ll show you a way to work around that issue. Just be patient. (Actually, you don\u2019t even need to be all that patient; after all, we\u2019re just about done with the post-Windows 2000 version.)<\/P>\n<P>Once we get back our collection of network printers we set up a For Each loop to loop through each of the items in that collection. For each item all we have to do is call the <B>Delete_<\/B> method and that printer connection will be removed:<\/P><PRE class=\"codeSample\">objPrinter.Delete_\n<\/PRE>\n<P>Tah-dah!<\/P>\n<P>Now, what about the Windows 2000 version? Well, this is bit trickier, and for two reasons. First, and as we noted earlier, the Network property doesn\u2019t exist in Windows 2000; that makes it harder to identify which printers are network printers and which ones aren\u2019t. Second, it\u2019s not just the Network property that the Win32_Printer class doesn\u2019t support; Win32_Printer doesn\u2019t support the Delete_ method, either. That means we have to do a bit of a workaround just to get the collection of network printers in the first place, and then we have to hand that collection off to Windows Script Host (WSH) and let <I>it<\/I> do the deleting for us.<\/P>\n<TABLE id=\"EUG\" 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>. This also means that the Windows 2000 version of the script can only be run on the local computer. That\u2019s because the WSH methods for adding\/removing printer connections can\u2019t be used against remote machines.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>For better or worse, here\u2019s the Windows 2000 script:<\/P><PRE class=\"codeSample\">Const NETWORK = 22<\/p>\n<p>Set objNetwork = CreateObject(&#8220;WScript.Network&#8221;)<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colPrinters = objWMIService.ExecQuery(&#8220;Select * From Win32_Printer&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    If objPrinter.Attributes And NETWORK Then \n        strPrinter = objPrinter.Name\n        objNetwork.RemovePrinterConnection strPrinter\n    End If\nNext\n<\/PRE>\n<P>This time around we start out by defining a constant NETWORK and setting the value to 22; we\u2019ll use this constant to help us determine whether a given printer is a network printer or a local printer. After that we create an instance of the <B>Wscript.Network<\/B> object, the WSH object we can use to remove printers from a computer.<\/P>\n<P>From there we bind to the WMI service (remember, local computer only) and then use the following line of code to return a collection of <I>all<\/I> the printers on a computer:<\/P><PRE class=\"codeSample\">Set colPrinters = objWMIService.ExecQuery(&#8220;Select * From Win32_Printer&#8221;)\n<\/PRE>\n<P>Now, you\u2019re right: we don\u2019t <I>want<\/I> to remove all the printers on a computer, do we? Instead, we want to remove only network printers. That\u2019s why, after setting up a For Each loop to walk through the collection of printers, we run this line of code:<\/P><PRE class=\"codeSample\">If objPrinter.Attributes And NETWORK Then\n<\/PRE>\n<P>As it turns out, the <B>Attributes<\/B> property is a \u201cbitmask\u201d property, a property that can contain more than one value. (For more information about bitmask properties see <A href=\"http:\/\/msevents.microsoft.com\/cui\/eventdetail.aspx?EventID=1032257681&amp;Culture=en-US\" target=\"_blank\"><B>this Scripting Guys webcast<\/B><\/A>.) Bitmasks are composed of \u201cswitches\u201d that can be either on or off; for example, the Attributes property has a \u201cswitch\u201d with a value of 22 that tells us whether a printer is a network printer. If this switch is on then we have a network printer; if this switch is off then we have a local printer. The preceding line of code simply checks to see if the Network switch is on. If it is, we assign the printer <B>Name<\/B> to a variable named strPrinter, then use this line of code to delete that printer connection:<\/P><PRE class=\"codeSample\">objNetwork.RemovePrinterConnection strPrinter\n<\/PRE>\n<P>From there we loop around and repeat the process with the next printer in the connection. It\u2019s not the most elegant script in the world, and it <I>is<\/I> restricted to the local machine (making it a candidate for inclusion in a logon script) but it works.<\/P>\n<P>By the way, local printers have their own switch, one with a value of 100. How does that help you search for <I>local<\/I> printers? Well, to search for local printers you can first define a constant named LOCAL, with a value of 100:<\/P><PRE class=\"codeSample\">Const LOCAL = 100\n<\/PRE>\n<P>And then you can check each printer on the computer to see if the local switch is on or off:<\/P><PRE class=\"codeSample\">If objPrinter.Attributes And LOCAL Then\n<\/PRE>\n<P>In the meantime, it\u2019s back to work for the Scripting Guy who writes this column. As you might expect, we\u2019re looking forward to talking to as many people as we can in Barcelona; if you\u2019re attending TechEd IT Forum take a minute or two to swing by booth 22 in the Ask the Experts section. Sure, you\u2019ll have to spend a minute chatting with a Scripting Guy, but maybe you\u2019ll get lucky: maybe you\u2019ll get to talk to Jean instead of the Scripting Guy who writes this column. Either way, you\u2019ll also get a copy of Dr. Scripto\u2019s Fun Book, you\u2019ll get a bonus CD with <I>hundreds<\/I> of scripting-related columns and articles (plus a scripting song or two), and you\u2019ll have a chance to win one of the few remaining Dr. Scripto bobblehead dolls. What more could you ask?<\/P>\n<P>Actually, that\u2019s a really good idea and someone <I>should<\/I> ask her about that. But not us<I>;<\/I> as it is, the Scripting Guy who writes this column has decided it might be best to wait a day or two before approaching with any new ideas or projects.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is it possible to delete or remove all network printers from a workstation with a VBScript script, similar to the way you removed all the network drives on a computer?&#8212; WDC Hey, WDC. If you\u2019ve been following our daily TechEd preview report, then you know that the Scripting Guys have been busy [&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-63663","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! Is it possible to delete or remove all network printers from a workstation with a VBScript script, similar to the way you removed all the network drives on a computer?&#8212; WDC Hey, WDC. If you\u2019ve been following our daily TechEd preview report, then you know that the Scripting Guys have been busy [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63663","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=63663"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63663\/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=63663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}