{"id":68393,"date":"2005-12-06T20:28:00","date_gmt":"2005-12-06T20:28:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/12\/06\/how-can-i-print-text-files-to-a-printer-other-than-the-default-printer\/"},"modified":"2005-12-06T20:28:00","modified_gmt":"2005-12-06T20:28:00","slug":"how-can-i-print-text-files-to-a-printer-other-than-the-default-printer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-print-text-files-to-a-printer-other-than-the-default-printer\/","title":{"rendered":"How Can I Print Text Files to a Printer Other than the Default Printer?"},"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 print text files to a printer other than the default printer?<BR><BR>&#8212; JD<\/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, JD. Boy, talk about pride going before the fall. When we first set out to answer this question we already \u201cknew\u201d how to solve the problem. We were well aware that the Shell object provides a way for you to <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/oct04\/hey1004.mspx\"><B>print text files<\/B><\/A> to the default printer; we were also well aware that this was the <I>only<\/I> printer to which you could print text files. But, being the Scripting Guys and all, we figured we had a clever workaround for this problem: we\u2019d tell everyone to use Microsoft Word to print out the text files. And then we\u2019d just sit back and listen to everyone tell us how smart we were.<\/P>\n<P>Oops. It was only after we started writing up our answer that we actually bothered to check and see if Microsoft Word actually <I>did<\/I> allow you to choose an alternate printer when printing documents from a script. As it turns out, it doesn\u2019t: Word requires you to print to the default printer as well. As painful as it is to admit this, the Scripting Guys were <I>wrong<\/I>.<\/P>\n<TABLE id=\"EKD\" 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>. OK, true: we\u2019re wrong <I>lots<\/I> of times. But that doesn\u2019t make it any less painful.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As you might expect, this then became a matter of pride: we <I>had<\/I> to find a way to print a text file to a different printer. Unfortunately, we couldn\u2019t find such a way. Therefore, we opted to go with Plan B: if we can print only to the default printer, well, then we\u2019ll just change the default printer, print the file, and then change back. Here\u2019s a solution that will work on Windows XP and Windows Server 2003:<\/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 colPrinters =  objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Printer Where Default = TRUE&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    strOldDefault = objPrinter.Name\n    strOldDefault = Replace(strOldDefault, &#8220;\\&#8221;, &#8220;\\\\&#8221;)\nNext<\/p>\n<p>Set colPrinters =  objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Printer Where Name = &#8216;\\\\\\\\atl-ps-01\\\\printer2&#8242;&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    objPrinter.SetDefaultPrinter()\nNext<\/p>\n<p>Wscript.Sleep 2000<\/p>\n<p>TargetFolder = &#8220;C:\\Logs&#8221; \nSet objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(TargetFolder) \nSet colItems = objFolder.Items\nFor Each objItem in colItems\n    objItem.InvokeVerbEx(&#8220;Print&#8221;)\nNext<\/p>\n<p>Set colPrinters =  objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Printer Where Name = &#8216;&#8221; &amp; strOldDefault &amp; &#8220;&#8216;&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    objPrinter.SetDefaultPrinter()\nNext\n<\/PRE>\n<TABLE id=\"E5D\" 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>. Why only Windows XP and Windows Server 2003? Well, in prior versions of Windows it\u2019s not real easy to identify &#8211; and change &#8211; the default printer; that\u2019s because the WMI class Win32_Printer has neither a <B>Default<\/B> property nor a <B>SetDefaultPrinter()<\/B> method. Therefore, we decided to go with the XP\/2003 solution for today; if there\u2019s enough interest in doing a Windows 2000 version, well, we\u2019ll see what we can come up with.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Admittedly, this script might seem a tiny bit complicated; bear with us, though, and you\u2019ll see what we\u2019re doing and why. We begin by connecting to the WMI service on the local computer and then retrieving the <B>Name<\/B> of the default printer. That\u2019s what we do here:<\/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 colPrinters =  objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Printer Where Default = TRUE&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    strOldDefault = objPrinter.Name\n    strOldDefault = Replace(strOldDefault, &#8220;\\&#8221;, &#8220;\\\\&#8221;)\nNext\n<\/PRE>\n<P>The first part of this code snippet should be self-explanatory; the only semi-tricky part lies in our WQL query. We want information only about the default printer so we construct a query that returns data for printers where the Default property is equal to True. Because there can be only one default printer on a computer, this query should return just one printer.<\/P>\n<P>The second part of the snippet &#8211; the For Each loop &#8211; might take a little bit more explaining. In the For Each loop we retrieve the Name of the default printer (remember, that\u2019s the only printer in the collection) and store it in a variable named strOldDefault. So far so good. We then have this line of code:<\/P><PRE class=\"codeSample\">strOldDefault = Replace(strOldDefault, &#8220;\\&#8221;, &#8220;\\\\&#8221;)\n<\/PRE>\n<P>Why? Well, if the default printer happens to be a network printer it\u2019s going to have a name like this: <B>\\\\atl-ps-01\\printer1<\/B>. That\u2019s fine, except that later on in the script we need to use that printer name in a WQL Where clause. Because the \\ is a reserved character, we have to \u201cescape\u201d each \\ character by doubling it; in other words, our printer &#8211; for purposes the Where clause &#8211; must have the name <B>\\\\\\\\atl-ps-01\\\\printer1<\/B>. We use the VBScript <B>Replace<\/B> function to replace any \\ in the printer name with a pair of \\\u2019s.<\/P>\n<P>Got that? Good. Next we have this block of code:<\/P><PRE class=\"codeSample\">Set colPrinters =  objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Printer Where Name = &#8216;\\\\\\\\atl-ps-01\\\\printer2&#8242;&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    objPrinter.SetDefaultPrinter()\nNext<\/p>\n<p>Wscript.Sleep 2000\n<\/PRE>\n<P>Here we\u2019re requesting a new collection, this one consisting of all printers with the name \\\\\\\\atl-ps-01\\\\printer2 (note the doubled-up \\\u2019s); as you might have guessed, this is the printer we want to print to. We retrieve this collection (which, again, will have only one member), then use the SetDefaultPrinter() method to make \\\\atl-ps-01\\printer2 the default printer. We then pause the script for two seconds, just to make sure the change is made.<\/P>\n<P>Now that we have a new default printer we can go ahead and print our text file. We\u2019ve taken this code directly from a previous <I>Hey, Scripting Guy!<\/I> column, the one on <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/oct04\/hey1004.mspx\"><B>printing text files<\/B><\/A>. We won\u2019t talk about the code here; see the column on printing text files if you need more information:<\/P><PRE class=\"codeSample\">TargetFolder = &#8220;C:\\Logs&#8221; \nSet objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(TargetFolder) \nSet colItems = objFolder.Items\nFor Each objItem in colItems\n    objItem.InvokeVerbEx(&#8220;Print&#8221;)\nNext\n<\/PRE>\n<P>So what have we done so far? Well, we\u2019ve determined the name of the default printer (for this discussion we\u2019ll assume it\u2019s \\\\atl-ps-01\\printer1) and stored it in a variable named strOldDefault. We\u2019ve <I>changed<\/I> the default printer to \\\\atl-ps-01\\printer2 and printed our text files. Now all we have to do is restore the old default printer, something we do with this block of code:<\/P><PRE class=\"codeSample\">Set colPrinters =  objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Printer Where Name = &#8216;&#8221; &amp; strOldDefault &amp; &#8220;&#8216;&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    objPrinter.SetDefaultPrinter()\nNext\n<\/PRE>\n<P>As you can see, here we connect to the old default printer; that is, we request a collection consisting of all the printers that have a Name property equal to the value stored in the variable strOldDefault. As soon as we get that collection we then use the SetDefaultPrinter() method to reset \\\\atl-ps-01\\printer1 as the default printer. The net result: we printed our text files to printer2, yet printer1 remains the default printer. It might not be a particularly clever solution, but it works. <\/P>\n<P>Besides, we\u2019ve learned our lesson about trying to be <I>too<\/I> clever. We promise never to try and do anything smart again (which is one promise we can probably keep).<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I print text files to a printer other than the default printer?&#8212; JD Hey, JD. Boy, talk about pride going before the fall. When we first set out to answer this question we already \u201cknew\u201d how to solve the problem. We were well aware that the Shell object provides a [&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,4,14,5],"class_list":["post-68393","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-printing","tag-printing","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I print text files to a printer other than the default printer?&#8212; JD Hey, JD. Boy, talk about pride going before the fall. When we first set out to answer this question we already \u201cknew\u201d how to solve the problem. We were well aware that the Shell object provides a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68393","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=68393"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68393\/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=68393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}