{"id":64443,"date":"2007-07-17T00:51:00","date_gmt":"2007-07-17T00:51:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/07\/17\/how-can-i-list-the-network-printers-for-the-logged-on-user\/"},"modified":"2007-07-17T00:51:00","modified_gmt":"2007-07-17T00:51:00","slug":"how-can-i-list-the-network-printers-for-the-logged-on-user","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-list-the-network-printers-for-the-logged-on-user\/","title":{"rendered":"How Can I List the Network Printers For the Logged-On User?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I create a logon script that lists the network printers for the logged-on user, then writes that information to a text file?<BR><BR>&#8212; RC <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, RC. You know, the nice thing about coming to work after a few days off is \u2013 well, OK, so maybe we can\u2019t actually think of anything nice about coming to work after a few days off. If you happen to work at Microsoft, however, you\u2019re at least likely to hear something interesting the moment you get back. For example, this morning the Scripting Guy who writes this column was in the bathroom when he overheard the following conversation:<\/P>\n<P>\u201cSo how did <I>he<\/I> get a window office?\u201d<\/P>\n<P>\u201cI had to give him a window office. He told me he gets violent and angry if he doesn\u2019t have a window office. I didn\u2019t want him to get violent and angry, so I gave him a window office.\u201d<\/P>\n<P>Needless to say, window offices are more valuable than gold around here; for the most part they\u2019re only awarded to the best and the brightest. (And, for some reason, to the Scripting Editor, who also has a window office.) As you might expect, the Scripting Guy who writes this column <I>doesn\u2019t<\/I> have a window office. Of course, up until now, he wouldn\u2019t <I>expect<\/I> to have a window office; after all, his name rarely comes up any time people talk about the best and the brightest. But apparently you don\u2019t <I>have<\/I> to be among the best and the brightest, at least not as long as you\u2019re willing to be violent and angry. Hmmm \u2026.<\/P>\n<P>Ah, who are we trying to kid? For one thing, the Scripting Guy who writes this column is too lazy to be violent. For another, how could he ever be angry, especially when he has the opportunity to do things like write a logon script that can list all the network printers for the logged-on user:<\/P><PRE class=\"codeSample\">Const ForWriting = 2<\/p>\n<p>Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)<\/p>\n<p>strName = objNetwork.UserName\nstrDomain = objNetwork.UserDomain\nstrUser = strDomain &amp; &#8220;\\&#8221; &amp; strName<\/p>\n<p>strText = strUser &amp; vbCrLf<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colPrinters = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Printer Where Local = FALSE&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    strText = strText &amp; objPrinter.Name &amp; vbCrLf\nNext<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objFile = objFSO.CreateTextFile _\n    (&#8220;C:\\Scripts\\Printers.txt&#8221;, ForWriting, True)<\/p>\n<p>objFile.Write strText<\/p>\n<p>objFile.Close\n<\/PRE>\n<P>Two quick caveats before we explain how the script works. First, in its current incarnation this script won\u2019t work on Windows 2000 or any earlier versions of Windows; that\u2019s because those flavors of Windows don\u2019t include the Local property as part of the WMI class Win32_Printer. But don\u2019t get violent and angry; before we call it a day we\u2019ll show you a modified script that <I>will<\/I> work on Windows 2000.<\/P>\n<P>Second, this script simply logs the information to a text file named C:\\Scripts\\Printers.txt; each time the script runs it overwrites the information in Printers.txt with the most recent collection of printer information. That\u2019s fine, but it might not be the behavior you had in mind. If that\u2019s the case then you\u2019ll have to modify the code that writes to the text file. But, again, there\u2019s no need to get violent and angry (violent or angry, maybe, but not violent <I>and<\/I> angry). After all, we have <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/textfiles.mspx\"><B>plenty of information<\/B><\/A> about working with text files. (And that doesn\u2019t even include the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_scr_iikh.mspx\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>, which also has information about working with text files.)<\/P>\n<P>OK, <I>now<\/I> we\u2019ll explain how the script works. To begin with, with define a constant named ForWriting and set the value to 2; we\u2019ll use this constant when we write the printer information to the text file. Following that we create an instance of the <B>Wscript.Network<\/B> object, then execute these three lines of code:<\/P><PRE class=\"codeSample\">strName = objNetwork.UserName\nstrDomain = objNetwork.UserDomain\nstrUser = strDomain &amp; &#8220;\\&#8221; &amp; strName\n<\/PRE>\n<P>What are we doing here? Well, all we\u2019re doing is determining the name of the logged-on user (using the <B>UserName<\/B> property) and the domain of the logged-on user (<B>UserDomain<\/B>); that\u2019s what the first two lines of code are for. In line three we simply concatenate the user name and domain, giving us a user name in the format <I>DOMAIN\\username<\/I>. In other words:<\/P><PRE class=\"codeSample\">FABRIKAM\\kenmyer\n<\/PRE>\n<P>We then take this information plus a carriage return-linefeed (using the VBScript constant <B>vbCrLf<\/B>) and stash it in a variable named strText:<\/P><PRE class=\"codeSample\">strText = strUser &amp; vbCrLf\n<\/PRE>\n<P>After that we connect to the WMI service on the local computer (because this is a logon script we don\u2019t care about connecting to the WMI service on a remote computer). We then use this line of code to retrieve a collection of all the network printers on the computer:<\/P><PRE class=\"codeSample\">Set colPrinters = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Printer Where Local = FALSE&#8221;)\n<\/PRE>\n<P>Here we\u2019re simply selecting all the instances of the <B>Win32_Printer<\/B> class, or at least those instances where the <B>Local<\/B> property is False. As you can probably guess, that results in a collection of all the network printers; if the Local property was True we\u2019d have a local printer rather than a network printer. That\u2019s why the script won\u2019t work on Windows 2000; the Win32_Printer class in Windows 2000 doesn\u2019t support the Local property.<\/P>\n<P>But don\u2019t worry: we said we\u2019d show you an alternate script that <I>will<\/I> work on Windows 2000 and, by golly, we\u2019re going to do just that.<\/P>\n<P>But not yet. Before we do that, let\u2019s finish explaining how the original script works. After we retrieve the printer information we set up a For Each loop that lets us walk through the entire collection; for each printer in the collection we use this line of code to add the <B>Name<\/B> of the printer to the variable strText:<\/P><PRE class=\"codeSample\">strText = strText &amp; objPrinter.Name &amp; vbCrLf\n<\/PRE>\n<P>This should be pretty easy to follow: all we\u2019re doing is assigning a new value to strText. And just what <I>is<\/I> that new value? That\u2019s easy: it\u2019s the existing value of strText <I>plus<\/I> the name of the printer <I>plus<\/I> a carriage return-linefeed. <\/P>\n<P>The rest is easy. First we create an instance of the <B>Scripting.FileSystemObject<\/B> object; we then use the <B>CreateTextFile<\/B> method to create a new text file named C:\\Scripts\\Printers.txt:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.CreateTextFile _\n    (&#8220;C:\\Scripts\\Printers.txt&#8221;, ForWriting, True)\n<\/PRE>\n<P>Note the three parameters we pass to the CreateTextFile method:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The full path to the new file (C:\\Scripts\\Printers.txt).<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The constant ForWriting, which indicates that we want to write to the file.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The value True, which tells the script to overwrite an existing instance of C:\\Scripts\\Printers.txt. If the file Printers.txt exists then the script will overwrite it; if the file doesn\u2019t exist then the script will create it.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>After that we call the <B>Write<\/B> method to write the printer information to the file, then use the <B>Close<\/B> method to close the file. If we open the text file we should see information similar to this:<\/P><PRE class=\"codeSample\">FABRIKAM\\kenmyer\n\\\\atl-ps-01\\b43-2927-a\n\\\\atl-ps-01.fabrikam.com\\b43-2315-a\n\\\\atl-ps-02\\HP LaserJet 5MP\n<\/PRE>\n<P>Which is exactly what we wanted to see.<\/P>\n<P>Now, what about Windows 2000? Well, without explaining how this works, here\u2019s a logon script that will run on Windows 2000 and Windows NT:<\/P><PRE class=\"codeSample\">Const ForWriting = 2<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)<\/p>\n<p>strName = objNetwork.UserName\nstrDomain = objNetwork.UserDomain\nstrUser = strDomain &amp; &#8220;\\&#8221; &amp; strName<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>strText = strUser &amp; vbCrLf<\/p>\n<p>Set colPrinters = objWMIService.ExecQuery(&#8220;Select * From Win32_Printer&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    If Not objPrinter.Attributes And 64 Then \n        strText = strText &amp; objPrinter.Name &amp; vbCrLf\n    End If\nNext<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>Set objFile = objFSO.CreateTextFile _\n    (&#8220;C:\\Scripts\\Printers.txt&#8221;, ForWriting, True)<\/p>\n<p>objFile.Write strText<\/p>\n<p>objFile.Close\n<\/PRE>\n<P>The big difference here? This time we need to return <I>all<\/I> the printers, both local and network. We then check the value of the bitmask property <B>Attributes<\/B>. If the \u201cswitch\u201d equal to 64 is on then this is a local printer; if it\u2019s not, then this must be a network printer. In that case, we then add the Name of the printer to the variable strText.<\/P>\n<P>Incidentally, we need to issue a correction before we go. Earlier we stated that window offices were distributed to \u201cthe best and the brightest.\u201d As it turns out, window offices are distributed on the basis of seniority. In other words, the Scripting Editor didn\u2019t get a window office because she\u2019s really, really smart; instead, she got one because she\u2019s really, really old.<\/P>\n<P>Just don\u2019t tell her we said that.<\/P>\n<TABLE class=\"dataTable\" id=\"EHH\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Ed<\/B><B>itor\u2019s Note:<\/B> The fact that the Scripting Guy who writes this column is <I>much<\/I> older than the Scripting Editor sort of blows away that last theory. Unless we\u2019re talking about mental age, in which case the Scripting Editor has at least managed to reach adulthood, unlike the Scripting Guy who writes this column.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I create a logon script that lists the network printers for the logged-on user, then writes that information to a text file?&#8212; RC Hey, RC. You know, the nice thing about coming to work after a few days off is \u2013 well, OK, so maybe we can\u2019t actually think 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-64443","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 create a logon script that lists the network printers for the logged-on user, then writes that information to a text file?&#8212; RC Hey, RC. You know, the nice thing about coming to work after a few days off is \u2013 well, OK, so maybe we can\u2019t actually think of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64443","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=64443"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64443\/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=64443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}