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 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.
Why can't I use my duplexer?
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 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.
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.
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:
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 Not installed. 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...all to no avail.
This is an issue I have lived with since I upgraded from Windows 7 to Windows 8 to Windows 8.1. I really hoped that upgrading to Windows 8.1 would fix it, but alas, it did not.
Windows PowerShell to the rescue
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:
PS C:\> $hp = Get-Printer -Name *hp*
PS C:\> Set-PrinterProperty -PrinterName $hp.name -PropertyName Config:Duplexer -Value Installed
The previous script worked great for a while, then it quit working.
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 PropertyName 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 Printer Driver Developers Guide, and it makes for fascinating weekend reading.
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:
notepad (Get-PrinterDriver -Name *hp*).datafile
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:
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:
PS C:\> Set-PrinterProperty -PrinterName $hp.name -PropertyName "Config:DuplexUnit" -Value Installed
PS C:\> Get-PrintConfiguration -PrinterName $hp.name
PrinterName ComputerName Collate Color DuplexingMode
----------- ------------ ------- ----- -------------
HP2005DN True False TwoSidedLongEdge
Another way to find out about available printer properties is to use the Get-PrinterProperty cmdlet, as I use here:
PS C:\> Get-PrinterProperty -PropertyName $hp.name
cmdlet Get-PrinterProperty at command pipeline position 1
Then supply values for the following parameters:
PrinterName: PS C:\> Get-PrinterProperty -PrinterName $hp.name
ComputerName PrinterName PropertyName Type Value
------------ ----------- ------------ ---- -----
HP2005DN FormTrayTable String Config:AutoS...
HP2005DN Config:DuplexUnit String Installed
HP2005DN Config:Memory String 384MB
I can see from the previous output that the property is Config:DuplexUnit. In my previous testing, the Installed value is case sensitive (at least in my testing on my printer), so you may want to keep that in mind.
Note Of course this cmdlet requires Admin rights, so start the Windows PowerShell console with an elevated account.
Make the change automatic
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.
Creating a job to run at startup is pretty simple. For more information, read Use PowerShell to Create Job that Runs at Startup.
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:
PS C:\> $trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:45
PS C:\> Register-ScheduledJob -Trigger $trigger -ScriptBlock {Set-PrinterProperty -Printer
Name (Get-Printer -Name *hp*).name -PropertyName "Config:DuplexUnit" -Value Installed} -Na
me SetPrinterDuplexer
Id Name JobTriggers Command Enabled
-- ---- ----------- ------- -----
2 SetPrinterDu... 1 Set-PrinterProperty -PrinterName (Get... True
Now for the test
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:
Now for the big test. Is my printer set to duplex? I open Control Panel, navigate to my printer, click DeviceSettings, and sure enough, my duplexer is now installed. Sweet.
Two lines of script and I fixed an issue that has vexed me for two years. Not a bad ROI…not bad at all. Windows PowerShell for the win!
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy