Posts Tagged ‘automation’

Recently I was asked if you could use PowerShell to print to a specific printer.  I have written posts on how to print to your default printer but had never tried printing to a specific printer.  So I decided I should see if this is possible.

And what do you know there is a built in cmdlet called Out-Printer that accepts the -Name parameter.  Using this optional parameter allows you to specify an alternate printer.

OK so now that I’ve got that lets try it out.  I have a printer here in my office which is an HP 2600 color laserjet printer.  It is NOT my default printer so I would like to test printing to it using that cmdlet.

First lets just test the cmdlet without the optional parameter.

get-process | Out-Printer

Sure enough that prints the get-process output directly to my default printer.

Now lets try

get-process | Out-Printer -name \\Svr1\it-2600n

Sure enough this printed out to the HP 2600 color laser.  PERFECT!!!

Now there are a few problems with this cmdlet, the first being that if you specify the -name parameter and you are trying to print to a shared printer you need to use the UNC name of that printer.

The second problem is that this is a cmdlet that prints output.  It does not print the CONTENT of a file.  So for example if you wanted to print a txt document and you wrote Out-Printer “c:\test.txt” it would actually print out a page with c:\test.txt on it.

You could use the Get-Content cmdlet and pipe that into the Out-Printer cmdlet like this Get-Content “c:\test.txt” | Out-Printer

This would give you a printout of the actual contents of the test.txt file.  However, if you wanted to print a word document or a pdf document this would not work.  It will give you tons of garbage and not the actual file contents.

So you are back to the drawing board again.  To print a word or pdf document you need to use this command.

Start-Process -FilePath “C:\Test.Docx” -Verb Print

Which brings you back to the original issue that was made, how do I print to a printer that isn’t the default one?

And for the moment I have to say I don’t see a way.  I think you need to set the default printer first, then you can use the Start-Process cmdlet and print the document.

Read Full Post »

My office has a series of nightly reports from SQL Server that export as Adobe documents.  The users want them printed and waiting for them each morning on a specific printer.  We had a little utility that could do this (and more) but the problem with it was that the computer running the utility had to be logged in and the utility had to be launched.  Occasionally we would run into issues where the computer was rebooted and as a result the reports would not be printed out in the morning.

So, that got me to thinking.  This would be a great way to use PowerShell!  So here is what I did.

First I started experimenting with the Out-Printer cmdlet to see if I could use that.  However since a pdf document is not plain text I couldn’t use this command.

get-content c:\docstoprint\doc1.pdf | Out-Printer

All that would get me is several pages worth of useless garbage.

Then I found another command I could use.

Start-Process –FilePath “c:\docstoprint\doc1.pdf” –Verb Print

This worked exactly like I had hoped.  I was able to print the pdf document to my default printer.  With one side effect, Adobe was left open after the print job.  So I tried this option.

Start-Process –FilePath “c:\docstoprint\doc1.pdf” –Verb Print -PassThru | %{sleep 10;$_} | kill

The extra options told powershell to wait 10 seconds and then kill the process.  This worked perfectly.  So now all I needed to do was create a script to print out the list of documents and then set it up in Task Scheduler.

So here is my finished script.
Start-Process -FilePath “c:\docstoprint\doc1.pdf” –Verb Print -PassThru | %{sleep 10;$_} | kill

Start-Process -FilePath “c:\docstoprint\doc2.pdf” –Verb Print -PassThru | %{sleep 10;$_} | kill

Start-Process -FilePath “c:\docstoprint\doc3.pdf” –Verb Print -PassThru | %{sleep 10;$_} | kill

Start-Process -FilePath “c:\docstoprint\doc4.pdf” –Verb Print -PassThru | %{sleep 10;$_} | kill

Start-Process -FilePath “c:\docstoprint\doc5.pdf” –Verb Print -PassThru | %{sleep 10;$_} | kill
The last step is to actually schedule this in Windows Task Scheduler.

So, first launch Task Scheduler, right click on the Task Scheduler Library and select “Create Basic Task”



Give your task a meaningful name, I chose “Daily Automated Report Printout”.  And put a little description of the task so someone else will understand the purpose.  Then click Next.

The next step is to decide when you want the task to start.  In my case I need it every weekday at 6am.  So, I chose “Weekly” and then clicked next to select the time and days.   After selecting the days and time click Next.

Now you need to tell Task Scheduler what action you want to perform.  In this case you want to “Start a Program”


Click Next and then you need to tell Task Scheduler what program to start.



The program is powershell.exe and it should be located in C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Then in the “Add arguments” section you need to put in the path to the powershell script.  In my case it was c:\users\greg\documents\powershell\DocsToPrint.ps1

Click Next to finish the wizard.  You will be presented with a Summary of your new task, make sure everything is correct, be sure to click the “Open the Properties dialog” checkbox and then click Finish.   

In the Properties for the task you have one last step to configure. 

In the Security Options section on the General Tab, you need to change the option to “Run whether user is logged on or not”


Then when you click OK you will be asked to put in account credentials for running this task.  That way when the job is started it will have privileges to run the job and print out to the default printer.

Read Full Post »