{"id":70663,"date":"2005-01-13T16:07:00","date_gmt":"2005-01-13T16:07:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/01\/13\/how-can-i-map-a-printer-but-only-if-the-user-doesnt-have-a-local-printer\/"},"modified":"2005-01-13T16:07:00","modified_gmt":"2005-01-13T16:07:00","slug":"how-can-i-map-a-printer-but-only-if-the-user-doesnt-have-a-local-printer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-map-a-printer-but-only-if-the-user-doesnt-have-a-local-printer\/","title":{"rendered":"How Can I Map a Printer, But Only If the User Doesn\u2019t Have a Local Printer?"},"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! I\u2019d like to have a logon script that maps a network printer for each user <I>unless<\/I> that user already has a local printer. If they have a local printer, I don\u2019t want to add the network printer. Is that possible?<BR><BR>&#8212; AG<\/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, AG. Not only is that possible, but it\u2019s actually pretty easy, especially if you\u2019re running Windows XP or Windows 2003. We\u2019ve got two issues here: we need some code that maps a network printer, and we need some code that figures out whether or not the user has a local printer. Let\u2019s start with the code for mapping a network printer:<\/P><PRE class=\"codeSample\">Set objNetwork = CreateObject(&#8220;WScript.Network&#8221;)\nobjNetwork.AddWindowsPrinterConnection &#8220;\\\\PrintServer1\\Xerox300&#8221;\nobjNetwork.SetDefaultPrinter &#8220;\\\\PrintServer1\\Xerox300&#8221;\n<\/PRE>\n<P>If you\u2019re sitting there waiting patiently for the rest of the code, well, it\u2019s going to be a long wait: that\u2019s all the code you need to map a network printer. In the first line, we create an instance of the WSH Network object. In the second line, we use the <B>AddWindowsPrinterConnection<\/B> method to make a connection to the printer Xerox300 (found on the print server named PrintServer1). That\u2019s really all we need. Line 3, which makes the new printer the default printer, is optional. We added it just to show you how easy it is to make a printer the default printer.<\/P>\n<P>Now, what about determining whether or not there are any local printers connected to a computer? As it turns out, in Windows XP and Windows 2003 the WMI class Win32_Printer has a property named <B>Local<\/B>. If this property is TRUE, then the printer in question is a local printer; if this property is FALSE, then the printer in question is a network printer. If we want to know if there are any local printers attached to a computer, all we have to do is query for all the printers where the Local property is TRUE:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colPrinters = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Printer Where Local = TRUE&#8221;)\n<\/PRE>\n<P>We can then check the Count property to see how many local printers were found. If the Count equals 0, then no local printers were found. In that case, we\u2019d go ahead and map a network printer. If the Count is greater than 0, at least one local printer was found. In that case, we don\u2019t want to map a network printer, so we just don\u2019t do anything at all (something we Scripting Guys are incredibly good at).<\/P>\n<P>Here\u2019s a complete script that &#8211; on Windows XP and Windows 2003 &#8211; maps a network printer if no local printers are found:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colPrinters = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_Printer Where Local = TRUE&#8221;)<\/p>\n<p>If colPrinters.Count = 0 Then\n    Set objNetwork = CreateObject(&#8220;WScript.Network&#8221;)\n    objNetwork.AddWindowsPrinterConnection &#8220;\\\\PrintServer1\\Xerox300&#8221;\n    objNetwork.SetDefaultPrinter &#8220;\\\\PrintServer1\\Xerox300&#8221;\nEnd If\n<\/PRE>\n<P>The more observant (or paranoid) among you might have noticed that we keep talking about Windows XP and Windows 2003, but we\u2019ve been strangely silent when it comes to other versions of Windows. Does that mean you can\u2019t perform this task on, say, Windows 2000? No, it just means that &#8211; prior to Windows XP &#8211; the Win32_Printer class did not include a Local property. Consequently, we can\u2019t query for all printers where <B>Local = TRUE<\/B>.<\/P>\n<P>But that\u2019s OK; as usual, we can find a way around this limitation. Previous versions of the Win32_Printer <I>do<\/I> include a property named <B>Attributes<\/B>. One of the \u201cswitches\u201d housed within the Attributes property tells you whether or not the printer is a local printer. If this particular bit (equal to 64) is enabled, then the printer is local; if it is not enabled, then the printer is a network printer.<\/P>\n<P>Therefore, we can use some Boolean logic to test whether or not a computer has any local printers. We don\u2019t have time to explain Boolean logic today; for that you might want to check out <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_scr_tspz.mspx\" target=\"_blank\"><B>this section<\/B><\/A> of the Microsoft Windows 2000 Scripting Guide. In the meantime, here\u2019s a script that will work on Windows 2000:<\/P><PRE class=\"codeSample\">blnLocal = FALSE\nstrComputer = &#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 64 Then \n        blnLocal = TRUE\n    End If\nNext<\/p>\n<p>If blnLocal = FALSE\n    Set objNetwork = CreateObject(&#8220;WScript.Network&#8221;)\n    objNetwork.AddWindowsPrinterConnection &#8220;\\\\PrintServer1\\Xerox300&#8221;\n    objNetwork.SetDefaultPrinter &#8220;\\\\PrintServer1\\Xerox300&#8221;\nEnd If\n<\/PRE>\n<P>In this script, we set a variable named blnLocal to FALSE. We then retrieve a list of all the printers on a computer and check each one to see if the local printer bit has been set. That\u2019s what this line of code does:<\/P><PRE class=\"codeSample\">If objPrinter.Attributes And 64 Then\n<\/PRE>\n<P>If a local printer is found, we set the value of blnLocal to TRUE. At the end of the script, if blnLocal is TRUE, we know that a local printer was found and the script simply ends. If blnLocal is FALSE, however, that means that no local printer was found. Therefore, we go ahead and map the network printer.<\/P>\n<P>Whew! But it works.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019d like to have a logon script that maps a network printer for each user unless that user already has a local printer. If they have a local printer, I don\u2019t want to add the network printer. Is that possible?&#8212; AG Hey, AG. Not only is that possible, but it\u2019s actually pretty [&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-70663","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! I\u2019d like to have a logon script that maps a network printer for each user unless that user already has a local printer. If they have a local printer, I don\u2019t want to add the network printer. Is that possible?&#8212; AG Hey, AG. Not only is that possible, but it\u2019s actually pretty [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70663","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=70663"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70663\/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=70663"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70663"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70663"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}