How To Use PowerShell To Create PDF Files

PowerShell excels at exporting data, but it lacks native support for creating PDF files. Here are three techniques to overcome this limitation.

Brien Posey

February 1, 2024

3 Min Read
pdf key on the keyboard
Alamy

I have always appreciated PowerShell for its simplicity in exporting data to files. However, while PowerShell natively supports the creation of text and HTML files, it lacks a built-in ability to create PDF files. Despite this limitation, there are a few different methods for using PowerShell to generate PDF files.

Technique #1: Microsoft Office

Even though PowerShell doesn’t have built-in PDF capabilities, you can interact with Microsoft Office applications, which do have the ability to create PDF files.

There are plenty of online guides that explain how to use PowerShell to convert a Word document into a PDF file, but I couldn’t find anything on sending PowerShell-generated text to Microsoft Word, then saving that text as a PDF file. Somewhat surprisingly, it wasn’t very difficult to do.

First, we will need some text to output, so I am going to establish a variable named $Output and link it to the Get-Process cmdlet. $Output now contains a list of all the processes currently running on the system. This text is what I intend to export to a PDF file.

If you wish to apply this technique yourself, simply map the $Output variable to whatever text you want to save as a PDF file. Here’s the command I am using in this example:

$Output = Get-Process

The next line creates a COM object tied to the Microsoft Word application. The subsequent line makes the application visible on the screen so that you can see what is going on. Here are those commands:

Related:How to Export PowerShell Command History To Make a Script

$Word = New-Object -ComObject Word.Application 
$Word.Visible = $True

The following two lines handle the task of populating Microsoft Word with the text stored in the $Output variable. This is a two-step process. The first step is to create a document. So far, we have opened Word but have not opened or created a document. The second step is to paste (or in this case, tell PowerShell to type) the content of the $Output variable into the document. These are the commands:

$Doc = $Word.Documents.add() 
$Word.Selection.TypeText($Output)

Now that Word contains the text from PowerShell, we can save the document in PDF format. The command for this may appear a bit cryptic, but here it is:

$Doc.SaveAs([ref] ‘C:\temp\example.pdf’, [ref] 17)

Finally, the last step in the process is to clean up the objects that were created:

$Word.Close
$Word.Quit

Here is the script in its entirety:

$Output = Get-Process 
$Word = New-Object -ComObject Word.Application 
$Word.Visible = $True 
$Doc = $Word.Documents.add() 
$Word.Selection.TypeText($Output) 
$Doc.SaveAs([ref] ‘C:\temp\example.pdf’, [ref] 17) 
$Word.Close 
$Word.Quit

You can see the PowerShell console in Figure 1

PowerShell console showing commands used to send text to Microsoft Word and convert the text to PDF format

PowerShell PDF 1

Figure 1. These are the commands used to send text to Microsoft Word and convert it to PDF format.

Technique #2: Microsoft Edge

Using Microsoft Word for the PDF conversion is not the only option; another approach is to use Microsoft Edge.

The first technique I demonstrated, while effective when working purely with text data, may be less convenient when working with charts, images, or other graphical components. In that case, you may want to save the elements to an HTML file. You can then use the Edge browser to convert the file into PDF format.

PowerShell includes a cmdlet called Convert-ToHTML for outputting data in HTML format. From there, you can call msedge.exe and use various command-line switches to force Edge to save the HTML file as a PDF document.

While researching this process, I found a really nice script here. The line within that script that performs the actual conversion is:

Start-Process "msedge.exe" -ArgumentList @("--headless","--print-to-pdf=""$pdfPath""","--disable-extensions","--print-to-pdf-no-header","--disable-popup-blocking","--run-all-compositor-stages-before-draw","--disable-checker-imaging", "file:///$htmlPath")

Technique #3: A Pre-Built Script

One last option is to use a pre-built script to write PowerShell data to a PDF file. In the PowerShell Gallery, there is a community-developed script named Out-PDFFile.ps1 (find it here). You can download and use the script according to your needs. The script works by outputting data to a PDF printer.

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

http://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like