{"id":1798,"date":"2014-03-20T00:01:00","date_gmt":"2014-03-20T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/03\/20\/use-powershell-to-send-test-page-to-a-printer\/"},"modified":"2014-03-20T00:01:00","modified_gmt":"2014-03-20T00:01:00","slug":"use-powershell-to-send-test-page-to-a-printer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-send-test-page-to-a-printer\/","title":{"rendered":"Use PowerShell to Send Test Page to a Printer"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to send a test page to a printer.\nMicrosoft Scripting Guy, Ed Wilson, is here. This morning I am sipping an awesome orange-cream tea that I picked up at a tea shop in Leipzig, Germany. It smells wonderful, and it tastes just like a Dreamcicle that I used to eat when I was a kid. That orange candy-covered vanilla ice cream was just the thing for hot, humid days when I was growing up in Florida. I have absolutely fallen in love with this tea. &nbsp;\nAnyway, something else I have fallen in love with is all the printer cmdlets available in Windows PowerShell on Windows 8.1. I can do nearly everything I need to do to manage printers. The operative word, however, is nearly.\nAfter I add a printer, I like to print a test page&mdash;especially if I am going to be printing something important (which I believe, should be the only documents printed). But dude, I do not want to have to mouse around, open Control Panel, find the printer tool, and scramble until I find the print test page button. Instead, I simply want to use Windows PowerShell to print a test page. How hard is that? As it turns out, it is not very hard at all.<\/p>\n<h2>WMI to the rescue<\/h2>\n<p>I have always been able to print a test page from within a script. Even 15 years ago in the old VBScript days, I could print a test page. This is because Windows Management Instrumentation (WMI) has always had a print test page method for the printer class.<\/p>\n<h3>Win32_Printer class<\/h3>\n<p>The WMI class I need to use is the same WMI class that I used way back in the days of Windows&nbsp;NT 4.0. The class is Win32_Printer&hellip;talk about backwards compatibility. The cool thing about WMI since Windows PowerShell&nbsp;3.0 is that with the CIM cmdlets, using WMI is simple. In the VBScript days, it would have taken me nearly 30 minutes to write out a WMI script to send a test page to a printer. And in the end, it would have required about 18 lines of code.\nWith Windows PowerShell, it can be a one-line command. But anything that I do on a regular basis becomes an immediate candidate for a Windows PowerShell function. When I have a function, I tend to add it to my profile somewhere. In this case, I have a module that contains utility code, and this is where the function would reside.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> Tomorrow I am going to make a serious improvement to this function, and that will be the function I actually add to my utility module.\nI need to call the <b>PrintTestPage<\/b><i> <\/i>method from the Win32_Printer WMI class. With the CIM cmdlets, I have an <b>Invoke-CimMethod<\/b> cmdlet that is designed to invoke methods. The <b>Invoke-CimMethod<\/b> requires an instance of a printer object (something returned from Win32_Printer) before it can execute the method.\nThe easiest way to get this instance of the printer object is to use the <b>Get-CimInstance<\/b> cmdlet. All I need to do is to tell the <b>Get-CimInstance<\/b> cmdlet the name of my printer. Maybe I am not certain of the exact name of the printer. In that case, there is still no problem because I can use the <b>LIKE<\/b> operator to find the printer I need. This goes into the <b>&ndash;Filter<\/b> parameter, and it looks like this:<\/p>\n<p style=\"margin-left:30px\">Get-CimInstance win32_printer -Filter &#8220;name LIKE &#8216;$printer'&#8221;\nWhen I execute this code, I can then supply the returned printer object to the <b>InputObject<\/b> parameter of the <b>Invoke-CimMethod<\/b> cmdlet. I need to execute the <b>Get-CimInstance<\/b> cmdlet first, so I return a WMI printer object to the <b>Invoke-CimMethod<\/b> cmdlet.\nNo sweat. It works just like high-school algebra&mdash;stuff inside the parentheses executes before stuff outside the parentheses. Therefore, I put the <b>Get-CimInstance<\/b> command inside a pair of parentheses, and the I call <b>Invoke-CimMethod<\/b>. The method name is the same one that I used in Windows&nbsp;NT 4.0: <b>PrintTestPage<\/b>. Therefore, that is the value I supply to the <b>&ndash;MethodName<\/b> parameter. This command is shown here:<\/p>\n<p style=\"margin-left:30px\">Invoke-CimMethod -MethodName printtestpage -InputObject (<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; Get-CimInstance win32_printer -Filter &#8220;name LIKE &#8216;$printer'&#8221;)<\/p>\n<h2>Put it in a function<\/h2>\n<p>So I have my one-line command, and it works. Now I put it in a function. This is not hard. Basically, there are three things to do:<\/p>\n<ol>\n<li>Use the <b>Function<\/b> keyword.<\/li>\n<li>Add a function name.<\/li>\n<li>Add a script block.<\/li>\n<\/ol>\n<p>Dude, that&rsquo;s it. A Windows PowerShell function is as easy as one, two, three. I have a script block (that is my one-liner), so I am all set to create a function. I decide that I need to have a variable for the printer name&mdash;after all, a function with a hard-coded printer name is not all that useful. To do this, I use the <b>Param<\/b> statement, and I assign a variable to hold the printer name. This portion of the script is shown here:<\/p>\n<p style=\"margin-left:30px\">Param(<\/p>\n<p style=\"margin-left:30px\">&nbsp; [string]$printer = &#8220;hp2005dn&#8221;)\nNow I will add in the <b>Function<\/b> statement, name the function, and add my script block. The complete function is shown here:<\/p>\n<p style=\"margin-left:30px\">Function out-TestPage<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;Param(<\/p>\n<p style=\"margin-left:30px\">&nbsp; [string]$printer = &#8220;hp2005dn&#8221;)<\/p>\n<p style=\"margin-left:30px\">&nbsp;Invoke-CimMethod -MethodName printtestpage -InputObject (<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; Get-CimInstance win32_printer -Filter &#8220;name LIKE &#8216;$printer'&#8221;)<\/p>\n<p style=\"margin-left:30px\">}\nThat is it. With only a few lines of script, I have the ability to use Windows PowerShell to send a test page to my default printer. Pretty cool stuff. The only thing I do not like is figuring out what the name of the printer might be. So, I will fix that tomorrow by creating a dynamic parameter. This will add a bit of complexity, but it will be worth it for me in the end.\nUsing Windows PowerShell to print a test page will continue tomorrow when I will talk about creating a drop-down list to enumerate all printers on the target computer.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to send a test page to a printer. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping an awesome orange-cream tea that I picked up at a tea shop in Leipzig, Germany. It smells wonderful, and it tastes just like 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":[491,404,3,45],"class_list":["post-1798","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-printers","tag-printing","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to send a test page to a printer. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sipping an awesome orange-cream tea that I picked up at a tea shop in Leipzig, Germany. It smells wonderful, and it tastes just like a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1798","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=1798"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1798\/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=1798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}