I often have to convert Microsoft word documents to pdf. What if you have really many documents to convert, let’s say 1000 or more? Would you open each documents seperately and click on save? Bad idea. In this blog post I will give you a PowerShell function at hand which enables you to convert multiple documents with a single command. This procedure is called automation ;-). Let’s dive in.
The Objective
As already mentioned we want to convert multiple Microsoft Word documents to pdf. My cmdlet ConvertWordTo-PDF does exactly that.


Cool. By the way, you can also omit the destination parameter. Pdf files will then be saved in the source folder.

Download
Klick on this link to download the PowerShell module.
The Code
function ConvertWordTo-PDF {
<#
.SYNOPSIS
ConvertTo-PDF converts Microsoft Word documents to PDF files.
.DESCRIPTION
The cmdlet queries the given source folder including sub-folders to find *.docx and *.doc files,
converts all found files and saves them as pdf in the Destination folder. After completition, the Destination
folder with the newly created PDF files will be opened with Windows Explorer.
.PARAMETER SourceFolder
Mandatory. Enter the source folder of your Microsoft Word documents.
.PARAMETER DestinationFolder
Optional. Enter the Destination folder to save the created PDF documents. If you omit this parameter, pdf files will
be saved in the Source Folder.
.EXAMPLE
ConvertWordTo-PDF -SourceFolder C:\Temp -DestinationFolder C:\Temp1
ConvertWordTo-PDF -SourceFolder C:\temp
.NOTES
Author: Patrick Gruenauer | Microsoft PowerShell MVP [2018-2021]
Web: https://sid-500.com
#>
[CmdletBinding()]
param
(
[Parameter (Mandatory=$true,Position=0)]
[String]
$SourceFolder,
[Parameter (Position=1)]
[String]
$DestinationFolder = $SourceFolder
)
$i = 0
$word = New-Object -ComObject word.application
$FormatPDF = 17
$word.visible = $false
$types = '*.docx','*.doc'
If ((Test-Path $SourceFolder) -eq $false) {
throw "Error. Source Folder $SourceFolder not found." }
If ((Test-Path $DestinationFolder) -eq $false) {
throw "Error. Destination Folder $DestinationFolder not found." }
$files = Get-ChildItem -Path $SourceFolder -Include $Types -Recurse -ErrorAction Stop
''
Write-Warning "Converting Files to PDF ..."
''
foreach ($f in $files) {
$path = $DestinationFolder + '\' + $f.Name.Substring(0,($f.Name.LastIndexOf('.')))
$doc = $word.documents.open($f.FullName)
$doc.saveas($path,$FormatPDF)
$doc.close()
Write-Output "$($f.Name)"
$i++
}
''
Write-Output "$i file(s) converted."
Start-Sleep -Seconds 2
Invoke-Item $DestinationFolder
$word.Quit()
}
How to implement ConvertWordTo-PDF
Copy the code above into PowerShell ISE (ise.exe) or an editor of your choosing and run the code. Then type the command and have fun with it.
If you want to make the function permanently available, so that the function is available every time you start PowerShell, you have to create a folder in C:\Program Files\WindowsPowerShell\Modules. Name the folder ConvertWordTo-PDF. Then copy the psm1 file in that folder.
Credits
Credit goes to the author of the link below that inspired me and brought up the idea in me to create that function.
See also
- Empty the Recycle Bin with PowerShell and the Task Scheduler
- How to create a PowerShell Module with multiple Functions
- Create multiple DNS A Records with PowerShell
- Configure DHCP-Server-Role with PowerShell
- PowerShell: Creating functions that accept Pipeline Input
Categories: PowerShell
It doesnt work when run via sheduler. MS Word not opens. Any sugestions?
LikeLike
I’m a total novice to Powershell so troubleshooting a script, even if it’s not that complicated would take me quite some time to get into. That said, I’ve got an issue concerning only files which contain a “.” inside of the file name (example: date_Dr. example.docx). It doesn’t produce an error, but the output file of file type “file”, and not pdf.
Do you have an idea how to fix that?
LikeLike
Many thanks for this code.
When I run this code in windows schedule tasks, it does not work. CPU grows up to 100%.
Any idea what can be the issue ?
LikeLike
Wonderful explanation. I’m pretty sure that with your post here you have made it easier for many people to properly convert a Word document to PDF.
LikeLiked by 1 person
Thank you!
LikeLike
Hello,
When i run the script it disappears and not working for me, i am running it on windows 10, and i just ran another script for remove duplicates by you and worked perfectly.
please advise how to get this script working?
LikeLike
You need to implement the function as described at the end of the post. Best
LikeLike
Hi there, what i did was to download the function, quick virus-check, open the zip on WinRAR and extract the psm file directly into that location.. it worked a treat! Thank you so much Patrick Gruenauer, saved my bacon!
LikeLiked by 1 person
Thank you!
LikeLike