{"id":974,"date":"2014-07-20T00:01:00","date_gmt":"2014-07-20T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/07\/20\/weekend-scripter-use-powershell-to-fix-broken-printer\/"},"modified":"2014-07-20T00:01:00","modified_gmt":"2014-07-20T00:01:00","slug":"weekend-scripter-use-powershell-to-fix-broken-printer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-fix-broken-printer\/","title":{"rendered":"Weekend Scripter: Use PowerShell to Fix Broken Printer"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to fix a printer that keeps losing its duplexer.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about Windows PowerShell is that if I want to, I can generally use it to solve all of my issues. Sometimes the issue is not sufficient that I want to invest the time. However, if it is a persistent issue, and if it occurs often enough, at some point, it might very well tip the scale. The issue I have today is just such a case.<\/p>\n<h2>Why can&#039;t I use my duplexer?<\/h2>\n<p>I believe in printing on both sides of the paper when I have stuff to print out. This, I believe, is a judicious use of resources. Therefore, whenever I buy a printer, I must get one with a duplexer. The printer I have, worked fine, until I upgraded to Windows&nbsp;8. Now, for some reason, every time I reboot my computer, the printer forgets that it has a duplexer. I suspect this is because the maker of the printer did not update their printer driver.<\/p>\n<p>Three times a month, I go to a writers group, and I have to print a portion of my new book project for peer review. This means that, at a minimum, this issue comes back to haunt me on a recurring basis. If I open Word, I must save my work in Word, close Word, go find my printer, tell the printer it has a duplexer, open Word, find my document and open it, find my spot, and then go into the Word dialog box and tell it to print.<\/p>\n<p>All of this can be enormously monotonous. I have done it often enough that I even know the difference between a printer property and a printer preference. Here is the dialog box:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-20-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-20-14-01.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>As I can see from this screenshot, for some reason the printer believes that the duplexer is not installed. I can easily change that, but every time I reboot my computer, it resets to <b>Not installed<\/b>. I have tried everything I could think of. I have checked for newer printer drivers and I have gone to the print device itself to see if something changed&#8230;all to no avail.<\/p>\n<p>This is an issue I have lived with since I upgraded from Windows&nbsp;7 to Windows&nbsp;8 to Windows&nbsp;8.1. I really hoped that upgrading to Windows&nbsp;8.1 would fix it, but alas, it did not.<\/p>\n<h2>Windows PowerShell to the rescue<\/h2>\n<p>This little issue is an annoyance, not a real problem. But it is what prompted me to see how to add back in printer options via Windows PowerShell. In fact, I wrote a script to do this very thing:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $hp = Get-Printer -Name *hp*<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Set-PrinterProperty -PrinterName $hp.name -PropertyName Config:Duplexer -Value Installed<\/p>\n<p>The previous script worked great for a while, then it quit working.<\/p>\n<p>I decided I needed to do a bit of research as to how all this works. Unfortunately, there is no information about what a permissible <b>PropertyName<\/b> looks like in the Help. To be honest, however, after doing my research, I am not sure how it could be documented because the property names are set by the people who write the printer driver. I did find a very interesting document. It is the <a href=\"http:\/\/download.microsoft.com\/download\/1\/D\/8\/1D84C319-0082-4DEB-8708-856EF3CE21C7\/developing-v4-print-drivers.docx\" target=\"_blank\">Printer Driver Developers Guide<\/a>, and it makes for fascinating weekend reading.<\/p>\n<p>The property names themselves are the ones that are in the printer driver data file. So I decided to use Windows PowerShell to retrieve the file and open it in Notepad. The command I used is:<\/p>\n<p style=\"margin-left:30px\">notepad (Get-PrinterDriver -Name *hp*).datafile<\/p>\n<p>The file looks fascinating, and so I decided to take a picture of it. As fascinating as the file is, in the following image, I am on line 1579. The good thing is that I can use the search feature in Notepad to find the information I need. It is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-20-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-20-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>It looks to me like they changed the name of the feature from Duplexer to DuplexUnit. So, I make a little change. Perfect. It works! Here is the new script:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Set-PrinterProperty -PrinterName $hp.name -PropertyName &quot;Config:DuplexUnit&quot; -Value Installed<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-PrintConfiguration -PrinterName $hp.name<\/p>\n<p style=\"margin-left:30px\">PrinterName&nbsp;&nbsp;&nbsp;&nbsp; ComputerName&nbsp;&nbsp;&nbsp; Collate&nbsp;&nbsp;&nbsp; Color&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DuplexingMode<\/p>\n<p style=\"margin-left:30px\">&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;&nbsp;&nbsp;&nbsp; &#8212;&#8212;-&nbsp;&nbsp;&nbsp; &#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;-<\/p>\n<p style=\"margin-left:30px\">HP2005DN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; True&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; False&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TwoSidedLongEdge<\/p>\n<p>Another way to find out about available printer properties is to use the <b>Get-PrinterProperty<\/b> cmdlet, as I use here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-PrinterProperty -PropertyName $hp.name<\/p>\n<p style=\"margin-left:30px\">cmdlet Get-PrinterProperty at command pipeline position 1<\/p>\n<p>Then supply values for the following parameters:<\/p>\n<p style=\"margin-left:30px\">PrinterName: PS C:\\&gt; Get-PrinterProperty -PrinterName $hp.name<\/p>\n<p style=\"margin-left:30px\">ComputerName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PrinterName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; PropertyName&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Value<\/p>\n<p style=\"margin-left:30px\">&#8212;&#8212;&#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HP2005DN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FormTrayTable&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String&nbsp;&nbsp;&nbsp;&nbsp; Config:AutoS&#8230;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HP2005DN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Config:DuplexUnit&nbsp;&nbsp;&nbsp; String&nbsp;&nbsp;&nbsp;&nbsp; Installed<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HP2005DN&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Config:Memory&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String&nbsp;&nbsp;&nbsp;&nbsp; 384MB<\/p>\n<p>I can see from the previous output that the property is <b>Config:DuplexUnit<\/b>. In my previous testing, the <b>Installed<\/b> value is case sensitive (at least in my testing on my printer), so you may want to keep that in mind.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> Of course this cmdlet requires Admin rights, so start the Windows PowerShell console with an elevated account.<\/p>\n<h2>Make the change automatic<\/h2>\n<p>Now I have found the Windows PowerShell script I need to be able to make the change, and I want to make the change automatically each time my laptop reboots. Therefore, I want a startup job.<\/p>\n<p>Creating a job to run at startup is pretty simple. For more information, read <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-job-that-runs-at-startup\/\" target=\"_blank\">Use PowerShell to Create Job that Runs at Startup<\/a>.<\/p>\n<p>I decided to use a script block instead of a script because in reality I had a one-liner, and it also makes the job more portable. First I create my startup trigger, then I register the scheduled job. It is two lines of script:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:45<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Register-ScheduledJob -Trigger $trigger -ScriptBlock {Set-PrinterProperty -Printer<\/p>\n<p style=\"margin-left:30px\">Name (Get-Printer -Name *hp*).name -PropertyName &quot;Config:DuplexUnit&quot; -Value Installed} -Na<\/p>\n<p style=\"margin-left:30px\">me SetPrinterDuplexer<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-left:30px\">Id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; JobTriggers&nbsp;&nbsp;&nbsp;&nbsp; Command&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;Enabled<\/p>\n<p style=\"margin-left:30px\">&#8212;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8212;&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8211;<\/p>\n<p style=\"margin-left:30px\">2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SetPrinterDu&#8230; 1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Set-PrinterProperty -PrinterName (Get&#8230; True<\/p>\n<h2>Now for the test<\/h2>\n<p>I reboot my computer, and the first thing I do is open the Windows PowerShell console with Admin rights to see if my scheduled job ran. As shown here, it did:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-20-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-20-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Now for the big test. Is my printer set to duplex? I open Control Panel, navigate to my printer, click <b>Device<\/b> <b>Settings<\/b>, and sure enough, my duplexer is now installed. Sweet.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-20-14-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-7-20-14-04.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>Two lines of script and I fixed an issue that has vexed me for two years. Not a bad ROI&hellip;not bad at all. Windows PowerShell for the win!<\/p>\n<p>I 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=\"mailto: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.<\/p>\n<p><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 fix a printer that keeps losing its duplexer. Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about Windows PowerShell is that if I want to, I can generally use it to solve all of my issues. Sometimes the issue [&hellip;]<\/p>\n","protected":false},"author":596,"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,61,45],"class_list":["post-974","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-client-side-printing","tag-printing","tag-scripting-guy","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to fix a printer that keeps losing its duplexer. Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about Windows PowerShell is that if I want to, I can generally use it to solve all of my issues. Sometimes the issue [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/974","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=974"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/974\/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=974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}