{"id":66693,"date":"2006-08-14T13:55:00","date_gmt":"2006-08-14T13:55:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/08\/14\/how-can-i-list-all-the-printers-on-a-remote-computer\/"},"modified":"2006-08-14T13:55:00","modified_gmt":"2006-08-14T13:55:00","slug":"how-can-i-list-all-the-printers-on-a-remote-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-list-all-the-printers-on-a-remote-computer\/","title":{"rendered":"How Can I List All the Printers on a Remote 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! How can I list all the printers \u2013 both local printers and network printers \u2013 on a remote computer?<BR><BR>&#8212; CH<\/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, CH. You know, back when he worked at the University of Washington the Scripting Guy who writes this column was asked to speak at a symposium being held as part of a conference on computers and education. To say this was a last-minute invitation would be an understatement; in fact, our hero had to start putting together his talk on the plane ride from Seattle to Florida, then finish it when he reached the convention center. When he arrived at the symposium itself, he discovered that his fellow speakers were hardly as harried as he was. Their secret: instead of creating a new talk for the symposium they decided to just deliver talks that they were already giving at the conference. In other words, they were going to give the exact same talk on two different days, but to two different audiences (or so they hoped).<\/P>\n<TABLE id=\"E2C\" 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>. As a matter of fact it <I>was<\/I> a very interesting symposium, if only because the future Scripting Guy was the only person on the panel who had a talk even remotely connected to the topic. Needless to say, it proved to be the lowest-rated session at the conference. (And, let\u2019s face it: being voted the most-boring and least-useful session at an academic conference isn\u2019t easy.)<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Now, if this future Scripting Guy was smart he would have learned that clever people can do twice the work with half as much effort. Your question, CH, is a good example of that. Because you specifically asked about listing printers on a remote computer, a smart person would write a column about that, then come back a few months later and reprint the column, only this time telling people how to list printers on the <I>local<\/I> computer. (And a smart person could get away with that because the two scripts are all-but identical.) <\/P>\n<P>Of course, as people who read this column know, the Scripting Guy responsible for all this isn\u2019t particularly smart. Instead of writing two separate columns he could only come up with one, thanks to a script that lists the printers on <I>either<\/I> a remote computer or a local computer:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ps-01&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colPrinters = objWMIService.ExecQuery(&#8220;Select * From Win32_Printer&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    If objPrinter.Attributes And 64 Then \n        strPrinterType = &#8220;Local&#8221;\n    Else\n        strPrinterType = &#8220;Network&#8221;\n    End If\n    Wscript.Echo objPrinter.Name &amp; &#8221; &#8212; &#8221; &amp; strPrinterType\nNext\n<\/PRE>\n<P>As you can see, we begin by assigning the name of the remote computer (in this case, atl-ps-01) to a variable named strComputer. What if we <I>did<\/I> want to list the printers on the local machine? No problem, and no need to read a separate column, either. Instead, just assign strComputer the name of the local machine, or assign it a dot (WMI shorthand for the local computer):<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\n<\/PRE>\n<P>After connecting to the WMI service on the desired computer, we then issue the following query, a query that returns a collection of all the printers installed on the computer:<\/P><PRE class=\"codeSample\">Set colPrinters = objWMIService.ExecQuery(&#8220;Select * From Win32_Printer&#8221;)\n<\/PRE>\n<P>If you weren\u2019t sure just how \u2026 smart \u2026 the Scripting Guy who writes this column really is, well, what comes next should help convinced you. CH only asked for a list of printers, both local and network; however, no one mentioned whether or not the script could specify which printers were which. That could have been a whole \u2018nother column; instead, we simply added a few lines of code that will make that distinction for you.<\/P>\n<P>To make that distinction we first set up a For Each loop that loops through the collection of printers. Inside that loop the very first thing we do is execute this line of code:<\/P><PRE class=\"codeSample\">If objPrinter.Attributes And 64 Then\n<\/PRE>\n<P>What we\u2019re doing here is checking the bitmask property <B>Attributes<\/B> to see whether bit 64 has been set. If it has, then we know that this is a local printer; in that case we set the value of the variable strPrinterType to <I>Local<\/I>:<\/P><PRE class=\"codeSample\">strPrinterType = &#8220;Local&#8221;\n<\/PRE>\n<P>If bit 64 has <I>not<\/I> been set then this must be a network printer; consequently we assign strPrinterType the value <I>Network<\/I>:<\/P><PRE class=\"codeSample\">strPrinterType = &#8220;Network&#8221;\n<\/PRE>\n<P>Two notes about this process. First, we aren\u2019t going to talk about bitmask attributes today; for a reasonably good explanation of these babies take a look at <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_scr_tspz.mspx\"><B>this section<\/B><\/A> of the <I>Microsoft Windows 2000 Scripting Guide<\/I>..<\/P>\n<P>Second, you might be wondering why we didn\u2019t just check the values of the Win32_Printer properties <B>Local<\/B> and <B>Network<\/B>; after all, isn\u2019t the sole purpose of those properties to tell you whether a printer is a local printer or a network printer? Yes it is, and using these two properties is a bit easier and a bit more straightforward than using the Attributes property. However, the Local and Network properties are supported only on Windows XP and Windows Server 2003. By using the Attributes property we were able to create a generic script that will work on any version of Windows that supports WMI.<\/P>\n<P>Which, come to think of it, means we missed out on yet <I>another<\/I> column: How Can I Determine Printer Types on a Windows 2000 Computer? <\/P>\n<P>Sigh \u2026.<\/P>\n<P>All we have left to do now is echo back the printer name and the printer type:<\/P><PRE class=\"codeSample\">Wscript.Echo objPrinter.Name &amp; &#8221; &#8212; &#8221; &amp; strPrinterType\n<\/PRE>\n<P>And that should do it.<\/P>\n<P>Incidentally, a professor from the UW happened to attend the symposium where the future Scripting Guy spoke. As she told him the following day, she was <I>shocked<\/I>, absolutely shocked that the Scripting Guy had poked fun at the university. However, she added, \u201cSeeing as how you\u2019re not a professor and really not all that smart I\u2019m not going to report you to anyone.\u201d You can imagine how gratified this Scripting Guy was to hear that. <I>(Editor\u2019s Note: <\/I><I>P<\/I><I>eople <\/I><I>at Microsoft<\/I><I> say <\/I><I>pretty much <\/I><I>the same thing<\/I><I>s about this Scripting Guy<\/I><I>.<\/I><I> At least he\u2019s consistent.)<\/I><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I list all the printers \u2013 both local printers and network printers \u2013 on a remote computer?&#8212; CH Hey, CH. You know, back when he worked at the University of Washington the Scripting Guy who writes this column was asked to speak at a symposium being held as part of [&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-66693","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! How can I list all the printers \u2013 both local printers and network printers \u2013 on a remote computer?&#8212; CH Hey, CH. You know, back when he worked at the University of Washington the Scripting Guy who writes this column was asked to speak at a symposium being held as part of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66693","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=66693"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66693\/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=66693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}