Prof. Powershell

Perfect Printing

Nothing fancy --- the Out-Printer cmdlet provides simple printouts of console text. Of course, this might change.

Windows PowerShell ships with an Out-Printer cmdlet. The cmdlet currently only handles text files or formatted text that comes from the pipeline. For example, if you want to print a log file all you need is a command like this:

Get-content mylog.txt | out-printer

The file will be printed by your default printer. But you can also specify a printer by its name:

Get-content mylog.txt | out-printer \\print02\itprint04

You can print the results of any PowerShell expression in the same way:

get-process | where {$_.workingset -ge 20MB} | sort Workingset -desc | Select -first 10 | out-printer \\print02\itprint01

The cmdlet can't control things like portrait vs. landscape printing, but it does a decent job in getting what have displayed on your screen to a printed page. In fact you can't control the printer at all with this cmdlet. It also can't print images or anything other than text, which, I suppose,, makes sense because it is designed to take console output and print it. You can't view a graphic at the console. However, this cmdlet has been discussed among the MVP community so you never know how it might change.

But one way I especially like to use this cmdlet is with a PDF virtual printer. I've talked about this topic before. I have the BullZip PDF printer set up as my default printer. So when I run a command like this:

get-content servers.txt | foreach-object {get-wmiobject win32_computersystem -comp $_ | Select Name,Manufacturer,Model,SystemType,TotalPhysicalMemory,NumberofProcessors} | out-printer

a PDF file is created and I now have a physical inventory audit in a digital format.

Reminder: All PowerShell samples are single-line commands in this and all "Prof. PowerShell" columns. All quote marks, both single and double, are straight quotes. Sometimes the html formatting or your browser may misinterpret so be careful when copying and pasting.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events