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
- Hyper-V: Enabling VM Resource Metering
- Azure AD: How to set Maximum Devices per User
- PowerShell: Waiting for jobs to complete with Wait-Job
- Exchange Online: Message Tracking with PowerShell
- Active Directory: How to perform an Offline Domain Join
Categories: PowerShell
Hi, i created the folder and added the script and module file
and ran the module file then script but it is not working and also no error showing up.
Not sure what to do can you please help me.
LikeLike
Hi i tried to run this script but it is not doing anything. Can you please assist me?
LikeLike
Great Script, quick and easy. Just missing the “.pdf” at the End.
You have to change line 72 from
$path = $DestinationFolder + ‘\’ + $f.Name.Substring(0,($f.Name.LastIndexOf(‘.’)))
to
$path = $DestinationFolder + ‘\’ + $f.Name.Substring(0,($f.Name.LastIndexOf(‘.’)))+”.pdf”
then you also have the file type “.pdf” added on saveas.
BR
LikeLike
Thank you!
LikeLike
How would I convert from PDF to Word?
LikeLike
Insert
$SourceFolder = (Get-Item $SourceFolder).FullName
$DestinationFolder = (Get-Item $DestinationFolder).FullName
between der param() and $i = 0
That makes relative pathes posible:
ConvertWordTo-PDF .\word .\pdf
If anyone try this for pdf (it works!) – .visible = $false doesn’t work!
Good job and thank you.
LikeLiked by 1 person
Thank you!
LikeLike