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 text and HTML file creation, it lacks a built-in ability to create PDF files. Despite the limitation, you can use a few different methods in PowerShell to accomplish this.

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.

Plenty of online guides explain how PowerShell can convert a Word document into a PDF file. I couldn’t find anything on sending PowerShell-generated text to Microsoft Word and 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 will 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.

To apply this technique, 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, while the line after makes the application visible on the screen so you can see what is happening. 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 populate Microsoft Word with the text stored in the $Output variable. It is a two-step process:

  1. The first step is to create a document. So far, we have opened Word but have not opened or created a document.

  2. The second step is to paste (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 commands 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.

Related:PowerShell and AI: Create a Microsoft Word Copilot

PowerShell includes a cmdlet called ConvertTo-Html 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. The PowerShell Gallery contains a community-developed script named Out-PDFFile.ps1 (find it here). You can download and use the script depending on 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.

https://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