On occasion,
you may want to get information out of PowerShell onto an actual hard copy like,
yeah, actual paper that comes out of a printer.
You could, of course,
output it to a file using Out‑File and then open that file and print it off.
But PowerShell has a native command to send
PowerShell data directly to a printer, and that command is Out‑Printer.
Let's take a look.
As usual, let's explore the help to see if there's anything of interest in here.
So as we probably figured out, this command will send output to a printer.
It looks like we can specify the name of the printer and also,
as per all of the other out cmdlets,
you can use the InputObject parameter to specify the data
that this command is going to work with.
Under the description here,
you'll note it will send to the default printer if you don't specify a
name or an alternate printer if you do specify the name.
Now because I can't easily show you an actual piece of paper
coming out of my printer in the office here,
and I don't have that printer set up on this machine anyway,
I'm going to use a printer that we'll save as a PDF file.
But I just need you to use some imagination that instead of being a PDF file,
that the text is actually being sent to a real printer.
So I'll run Get‑Printer, and on this Windows 10 machine by default,
there's a printer here named Microsoft Print to PDF.
And if you haven't used this before,
you can literally just print to it as if it were a physical printer,
but it will allow you to save the document as a PDF file.
Let's run our trusty Get‑Process and pipe that to Out‑Printer. And I'm not
actually sure what my default printer currently is, so I'll use the name
parameter to specify the Microsoft Print to PDF printer.
If this were a physical printer, that would now simply start getting printed
out on a sheet of paper. But I'm going to get a prompt you to save this as a
PDF file. Let me drop it in the C docs folder, and I'll call the file
process.pdf. If I open that folder in Windows Explorer and then I'll just
open up the PDF file in Microsoft Edge here as I don't have a dedicated PDF
reader on this machine, and there you have it.
The default output of Get‑Process has been printed.
If we go back to the PowerShell console briefly,
I do want to mention that,
as per the other Out cmdlets, you can format the data first.
So if we wanted to print this as a list, we can pipe that across to
format‑list first and then send that to Out‑Printer.
I'll call this one process2.pdf, and then we can open that up. And sure enough this time, we do have a list view that's been printed off.