Use PowerShell to Print Output Without Installing Print Drivers

Use PowerShell to Print Output Without Installing Print Drivers

  • Comments 4
  • Likes

Summary: Use Windows PowerShell to print output from commands without installing print drivers.

Microsoft Scripting Guy, Ed Wilson, is here. If I have not said it recently, the Scripting Wife is cool! She is also a really good sport. Why do I say this? We are stuffed into the very back of a large airplane as we fly back from a Windows PowerShell User Group meeting. I did not get upgraded for this trip. While I love speaking in person at Windows PowerShell User Group meetings, flying across the United States nowadays takes a lot of time—it is therefore important for me to be able to maximize the travel time.

I publish a new Hey, Scripting Guy! blog every day, and that means if I take a day off from writing, I am a day behind. So, I constantly have my laptop with me, and I write wherever I am. So what does this have to do with the Scripting Wife? Well for one thing, she maintains my schedule. If you email me at scripter@microsoft.com and ask me to speak at your Windows PowerShell User Group, I will call up the Scripting Wife, and ask her to schedule it. She also makes all the travel arrangements.

Now, to the case in point…

We are stuffed in the back of a very large, completely full, totally cramped airplane. She saw that this was going to be the case as she monitored the flight during the week, and when she checked us in, she made certain she was sitting in front of me. “Huh?” you might ask. “You mean she did not want to sit with you?” Well, of course she did. She would most assuredly rather sit beside me, than beside the large, heat-radiating person who is currently drooling on her shoulder.

But she realized that if I was to have any hope at all of being able to use my laptop during the flight to write today’s Hey, Scripting Guy! blog, she would need to protect me from some fat dude laying in my lap and crushing my display when he reclines the chair. So she is sitting bolt upright on our flight to Chicago so I can work. Well done Scripting Wife! Today’s blog is dedicated to you!

Not every printer is a physical device

I am listening to the Rolling Stones on my Zune HD via my noise canceling headphones, trying to avoid a debilitating injury to my elbow as I type and dodge the flight attendants’ metal beverage cart that runs up and down the middle isle more often than the elevator at the Empire State Building in New York City. My laptop annoyed me for the last time this week, and so I F-Disked it and reinstalled Windows 7 Ultimate on it. Then we packed up and headed out town for a Windows PowerShell User Group meeting—so I have little more than Office 2010, all security updates, and my restored essential data (my ScriptingGuys folder and my Windows PowerShell modules folder). I did not even have time to install any printers. The image that follows displays my Printers folder.

Image of folder

The SnagIt printer installs with my screen capture software. If I am not certain of the printer names (I do not always trust things I see in graphical dialog boxes), I can use a quick WMI query to return the printer names. The command to obtain printer names from all my printers is shown here, along with the output associated with that command (in the command that follows, I use gwmi as an alias for Get-WmiObject, and select as an alias for Select-Object).

PS C:\> gwmi win32_printer | select name

 

name

----

SnagIt 8

Send To OneNote 2010

Microsoft XPS Document Writer

Fax

Use Windows PowerShell to print

I am whisking along at 35,000 feet, now listening to Cream on my Zune, and I decide I want to print. Well, besides the fact that I do not even have any real printers installed, and the fact that I do not have wireless access on the plane, I should mention that I shut my systems down at home before we left. So what can I do?

I can use the Out-Print Windows PowerShell cmdlet—it accepts piped input. “Say what?” you may ask. “You don’t have any real printers installed, and you couldn’t get to them even if you did.”

Did you notice the Microsoft XPS Document Writer? Microsoft XPS documents are portable documents, and I can create them by using the Out-Print cmdlet because the Microsoft XPS Document Writer acts like a real printer. In the command that follows, I use the Get-ChildItem (dir is an alias) to obtain a directory listing of the C:\fso folder. Instead of displaying the results directly to the Windows PowerShell console, I write it to an XPS document.

dir c:\fso | Out-Printer -Name "microsoft xps document writer"

When the command runs, a dialog box appears. The image that follows displays this dialog box.

Image of dialog box

I use the XPS Viewer to examine the output I wrote to the file. The image that follows shows the directory listing in the XPS Viewer.

Image of directory

If I need to do a lot of printing, I get tired of typing “Microsoft XPS Document Writer” all of the time. No problem; I can use Windows PowerShell and WMI to get the printer name, and to store it in a variable. Now I can use that value when I print. In the commands that follow, I use the Get-WmiObject cmdlet (gwmi is the alias). I use the Where-Object cmdlet to filter out the name that matches XPS. I store the returned WMI object in a variable, and I select the Name property with the Out-Printer cmdlet to print out process information (gps is alias for Get-Process).

$ptr = gwmi win32_printer | ? { $_.name -match 'xps'}

gps | Out-Printer $ptr.name

This technique works great when I need to document something—I simply pipe to the XPS printer. I can then email it to another machine, store the files, or collect them and send them to a remote printer all at once.

Well, that is about all there is to printing without having a real printer installed on the computer. Join me tomorrow when I begin a new week on the Hey, Scripting Guy! Blog.

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

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • What is the point of generating XPS when saving to a text file would give you the same advantages (it can be emailed to anyone, stored, archived, and printed easily), and some more (the files are smaller after compression, it's portable even to non-Windows OSes, and there is no worry of the format being left behind and forgotten in a few years (1)).

    (1) 'cause, seriously, XPS adoption has been really weak outside of Microsoft.

  • It's obviously not as cool as WMI - Duh!!

  • Hi Ed,

    well ..... I've got to aggree to Lionel as far as my empirical mind tells me, I've NEVER used the XPS printer! I even did forget that I've got that driver installed, but I have  :-()

    On the other side: I never used the Out-printer cmdlet either, so this was a good reminder!

    If I would use your approach I would get tired of typing “Microsoft XPS Document Writer” all of the time, too! But I'd like to go a step further and store the printername directly in $ptr

    $ptr = (gwmi win32_printer | ? { $_.name -match 'xps'}).Name

    or even set it as may standard printer as long as I don't have "something real" attached to my laptop:

    (gwmi win32_printer | ? { $_.name -match 'xps'}).SetDefaultprinter()

    That's really lazy :-)

    Klaus

  • I would like to go one step further:
    "Printing" multiple objects to corresponding xps files located in a certain default folder with unique file names (created by the PS script or random ones) — but *without* manually specifying the file names in the dialog box.
    Is there a way to bypass the dialog box by conveying the xps file names to the xps printer driver directly in the PS script?