Sending PowerShell Results to PDF (Part 1)

by Dec 20, 2018

Windows 10 and Windows Server 2016 finally come with a built-in PDF printer called “Microsoft Print to PDF” that you can use from PowerShell to create PDF files. Run this to check your PDF printer:

$printer = Get-Printer -Name "Microsoft Print to PDF" -ErrorAction SilentlyContinue
if (!$?)
{
    Write-Warning "Your PDF Printer is not yet available!"
}
else
{
    Write-Warning "PDF printer is ready for use."
}

If the printer is not (yet) available, then you are either not using Windows 10/Windows Server 2016, or the PDF Print Feature is not enabled. Run this command from an elevated PowerShell to fix it:

 
PS> Enable-WindowsOptionalFeature -Online -FeatureName Printing-PrintToPDFServices-Features 
 

To always make sure the line runs with Administrator privileges, you could also try and use this:

$code = 'Enable-WindowsOptionalFeature -Online -FeatureName Printing-PrintToPDFServices-Features'

Start-Process -Verb Runas -FilePath powershell.exe -ArgumentList "-noprofile -command $code"

Once your PDF Printer is ready for action, creating PDF files from PowerShell is very simple. Just send output to Out-Printer. Here is an example:

 
PS> Get-Service | Out-Printer -Name "Microsoft Print to PDF"
 

The printer driver opens a dialog where you can choose the output file name. If you’d like to print unattended and avoid this dialog, we’ll look into this tomorrow.


psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!