{"id":65863,"date":"2006-12-12T19:04:00","date_gmt":"2006-12-12T19:04:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/12\/12\/how-can-i-add-a-network-printer-at-logon-and-optionally-make-that-the-default-printer\/"},"modified":"2006-12-12T19:04:00","modified_gmt":"2006-12-12T19:04:00","slug":"how-can-i-add-a-network-printer-at-logon-and-optionally-make-that-the-default-printer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-add-a-network-printer-at-logon-and-optionally-make-that-the-default-printer\/","title":{"rendered":"How Can I Add a Network Printer at Logon and, Optionally, Make That the Default Printer?"},"content":{"rendered":"<p><img decoding=\"async\" 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\"><\/p>\n<p>Hey, Scripting Guy! How can I add a network printer at logon? I\u2019d also like to make that the default printer unless the user has a local printer. In that case I <i>don\u2019t<\/i> want to make the network printer the default printer.<\/p>\n<p>&#8212; DB<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><img decoding=\"async\" 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 decoding=\"async\" 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><\/p>\n<p>Hey, DB. So, how are \u2026 things \u2026.? That\u2019s really some kind of weather we\u2019ve\/you\u2019ve been having lately, isn\u2019t it?<\/p>\n<p>Sorry; this is proving to be a very difficult column for us to write. As we noted <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/dec06\/hey1211.mspx\"><b>yesterday<\/b><\/a>, the Scripting Guy who writes this column is on vacation; in fact, the column you\u2019re reading right now was actually written several days ago. You\u2019d think that this wouldn\u2019t make much difference when it comes to a daily scripting column; after all, there isn\u2019t a lot of breaking news in the scripting world. But we Scripting Guys don\u2019t want to be like other daily scripting columns; to help differentiate ourselves from the competition we like to sprinkle our columns with references to current events and to the outside world. But it\u2019s hard to reference current events when the column is being written in advanced, and those events haven\u2019t actually happened yet. <\/p>\n<table class=\"dataTable\" id=\"EGD\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. Well, sure, of course <i>Peter<\/i> can do it. But Peter, as the oldest living Scripting Guy, also has an unfair advantage: he\u2019s so <i>much<\/i> older than the rest of us that he\u2019s already seen everything that could ever happen, usually more than once. That\u2019s not true for youngsters like the other Scripting Guys.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>What that all means, of course, is that we have absolutely nothing to write about today. Sorry.<\/p>\n<p>Pardon? Write about <i>scripting<\/i>? Well, that seems a bit out-of-place for a daily scripting column. But, then again, we <i>are<\/i> a little desperate today. So what the heck:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"\nSet objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\nSet colPrinters = objWMIService.ExecQuery _\n    (\"Select * From Win32_Printer Where Local = TRUE\")\nSet objNetwork = CreateObject(\"WScript.Network\")\nobjNetwork.AddWindowsPrinterConnection \"\\\\PrintServer1\\Xerox300\"\nIf colPrinters.Count = 0 Then\n    objNetwork.SetDefaultPrinter \"\\\\PrintServer1\\Xerox300\"\nEnd If\n<\/pre>\n<p>As you can see, we start out by connecting to the WMI service on the local computer. Can we also use this script against <i>remote<\/i> computers? Actually no; that\u2019s because we\u2019re going to use Windows Script Host (WSH) to map the network printer, and WSH can perform that task on only the local machine. But that\u2019s OK, because, as a logon script, we don\u2019t <i>need<\/i> to use this against remote machines. Logon scripts always run locally.<\/p>\n<p>After connecting to the WMI service we use the following query to return a collection of all the local printers on the computer:<\/p>\n<pre class=\"codeSample\">Set colPrinters = objWMIService.ExecQuery _\n    (\"Select * From Win32_Printer Where Local = TRUE\")\n<\/pre>\n<p>Keep in mind that the preceding query (and thus the preceding script) works only on Windows XP, Windows Server 2003, and Windows Vista computers. Why? Because those are the only operating systems in which the <b>Win32_Printer<\/b> class includes the <b>Local<\/b> property. But don\u2019t fret; we\u2019ll show you a Windows 2000 version of this script in just a moment.<\/p>\n<p>After we issue our WQL query we create an instance of the <b>Wscript.Network<\/b> object, then use the <b>AddWindowsPrinterConnection<\/b> method to add the network printer \\\\PrintServer1\\Xerox300:<\/p>\n<pre class=\"codeSample\">Set objNetwork = CreateObject(\"WScript.Network\")\nobjNetwork.AddWindowsPrinterConnection \"\\\\PrintServer1\\Xerox300\"\n<\/pre>\n<p>This gives us a connection to our network printer. Now the question is this: should we make this printer the default printer? To answer that we need to know whether or not the user has any local printers installed. How do we know that? Remember the query we ran, the one that returned a collection of all the local printers? All we have to do is check the value of the collection\u2019s <b>Count<\/b> property. If the Count is equal to 0 that means no local printers are installed. In that case, we can then use this line of code to set this new printer as the default printer:<\/p>\n<pre class=\"codeSample\">objNetwork.SetDefaultPrinter \"\\\\PrintServer1\\Xerox300\"\n<\/pre>\n<p>If the Count is <i>not<\/i> equal to 0 that means we have at least one local printer installed. And so we do nothing at all.<\/p>\n<p>Speaking of nothing at all, we won\u2019t bother to explain how to perform this same task on a Windows 2000 computer. However, we\u2019ll at least give you a script that does so:<\/p>\n<pre class=\"codeSample\">blnLocal = FALSE\nstrComputer = \".\"\nSet objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")\nSet colPrinters = objWMIService.ExecQuery(\"Select * From Win32_Printer\")\nFor Each objPrinter in colPrinters\n    If objPrinter.Attributes And 64 Then\n        blnLocal = TRUE\n    End If\nNext\nSet objNetwork = CreateObject(\"WScript.Network\")\nobjNetwork.AddWindowsPrinterConnection \"\\\\PrintServer1\\Xerox300\"\nIf blnLocal = True Then\n    objNetwork.SetDefaultPrinter \"\\\\PrintServer1\\Xerox300\"\nEnd If\n<\/pre>\n<p>Incidentally, as we were writing this we realized, \u201cHey, we don\u2019t have to talk about <i>current<\/i> events. If something happened in the past we can bring it up and no one will ever notice.\u201d With that in mind, how about that French Revolution, huh? Was that crazy or <i>what<\/i>?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I add a network printer at logon? I\u2019d also like to make that the default printer unless the user has a local printer. In that case I don\u2019t want to make the network printer the default printer. &#8212; DB Hey, DB. So, how are \u2026 things \u2026.? That\u2019s really some [&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-65863","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 add a network printer at logon? I\u2019d also like to make that the default printer unless the user has a local printer. In that case I don\u2019t want to make the network printer the default printer. &#8212; DB Hey, DB. So, how are \u2026 things \u2026.? That\u2019s really some [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65863","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=65863"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65863\/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=65863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}