{"id":70073,"date":"2005-04-07T09:44:00","date_gmt":"2005-04-07T09:44:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/04\/07\/how-can-i-print-a-test-page-to-a-printer\/"},"modified":"2005-04-07T09:44:00","modified_gmt":"2005-04-07T09:44:00","slug":"how-can-i-print-a-test-page-to-a-printer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-print-a-test-page-to-a-printer\/","title":{"rendered":"How Can I Print a Test Page to a 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 a test page to a printer?<BR><BR>&#8212; RR<\/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, RR. As long as you\u2019re running Windows XP this is an easy one; that\u2019s because the revised <B>Win32_Printer<\/B> class introduced in Windows XP includes a method called <B>PrintTestPage<\/B>. As you might expect, this means you can bind to a given printer on a computer and then call the PrintTestPage method to, well, print a test page.<\/P>\n<P>Here\u2019s a sample script that does just that:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet 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 DeviceID = &#8216;\\\\\\\\atl-ps-01\\\\color-printer'&#8221;)<\/p>\n<p>For Each objPrinter in colPrinters\n    errReturn = objPrinter.PrintTestPage\n    If errReturn = 0 Then\n        Wscript.Echo &#8220;The test page was printed successfully.&#8221;\n    Else\n        Wscript.Echo &#8220;The test page could not be printed.&#8221;\n    End If\nNext\n<\/PRE>\n<P>All we do in this script is connect to the WMI service, then run a query that binds us to the printer with the <B>DeviceID<\/B> \\\\atl-ps-01\\color-printer. Note that because we are using the DeviceID as part of a Where clause (<B>Where DeviceID =<\/B>) we need to \u201cescape\u201d each \\ in the printer path. Consequently we need to use <I>this<\/I> as the DeviceID:<\/P><PRE class=\"codeSample\">\\\\\\\\atl-ps-01\\\\color-printer\n<\/PRE>\n<P>Just another one of the little eccentricities we\u2019ve come to love about WMI: any time you use a \\ in a Where clause you must preface it with a second slash. If you have back-to-back \\\\\u2019s like we do here, you must preface each slash with a second slash, yielding this crazy-looking construct: \\\\\\\\. But, rules are rules, right?<\/P>\n<P>After binding to the printer (and our collection will consist of only one printer, because DeviceIDs must be unique) we call the PrintTestPage method, capturing the return value in a variable named errReturn. If errReturn is 0 that means the test page printed successfully; if errReturn is anything <I>but<\/I> 0 then something went wrong and the test page could not be printed. Either way we echo the appropriate message to the screen.<\/P>\n<P>Like we said, pretty easy. What makes this especially nice is : 1) because this is WMI, we can print test pages from remote machines just as easily as we can from the local machine; and, 2) the return value will tell us whether or not the print test succeeded. No need to walk all the way to the printer only to find out that, for some reason, the print test failed.<\/P>\n<P>Of course, that\u2019s all well and good if you\u2019re running Windows XP. But what if you aren\u2019t, what if you\u2019re still running Windows 2000? Well, in that case the news isn\u2019t quite so good; we don\u2019t know of any \u201cpure\u201d scripting way to print a test page, at least not any way built into the operating system. However, you <I>can<\/I> print a test page from the command prompt using this command:<\/P><PRE class=\"codeSample\">rundll32 printui.dll,PrintUIEntry \/k \/n &#8220;\\\\atl-ps-01\\color-printer&#8221;\n<\/PRE>\n<P>If you wanted to, you could wrap this command up in a VBScript and execute the test page from there. For more information about running command line tools from within a script take a gander at this classic <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/tales\/sg1002.mspx\"><B>Tales from the Script<\/B><\/A> column (it\u2019s actually the first <I>Tales<\/I> ever written).<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I print a test page to a printer?&#8212; RR Hey, RR. As long as you\u2019re running Windows XP this is an easy one; that\u2019s because the revised Win32_Printer class introduced in Windows XP includes a method called PrintTestPage. As you might expect, this means you can bind to a given [&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,6],"class_list":["post-70073","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-printing","tag-printing","tag-scripting-guy","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I print a test page to a printer?&#8212; RR Hey, RR. As long as you\u2019re running Windows XP this is an easy one; that\u2019s because the revised Win32_Printer class introduced in Windows XP includes a method called PrintTestPage. As you might expect, this means you can bind to a given [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70073","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=70073"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70073\/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=70073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}