This is a little Powershell script that I came up with to convert a folder full of Word documents into PDF files. It takes a single command-line parameter, which is the directory containing the files that you wish to convert. It puts the PDF files in the same directory alongside the originals.
Copy the code below and place into a file named “WordToPdf.ps1” (or whatever you want to name it). Just use a text editor (like Notepad++) to create and save the file.
param([Parameter(Mandatory=$true)][string]$documents_path) $word_app = New-Object -ComObject Word.Application echo "Processing $($documents_path)" Get-ChildItem -Path $documents_path -Filter *.doc? | ForEach-Object { echo $_.FullName $document = $word_app.Documents.Open($_.FullName) $pdf_filename = "$($_.DirectoryName)\$($_.BaseName).pdf" $document.SaveAs([ref] $pdf_filename, [ref] 17) $document.Close() } $word_app.Quit()
Run the file like this:
.\WordToPdf.ps1 .\MyDirectoryOfDocs
After using this script for a while, I had need to process Word documents that spanned multiple directories. The following script still takes a single command-line parameter. This is the directory to process. However, the script will process Word documents in each of the first level directories found within this directory. It is NOT recursive. I’ll leave that to your imagination.
param([Parameter(Mandatory=$true)][string]$documents_path) $word_app = New-Object -ComObject Word.Application Get-ChildItem -Path $documents_path -Directory | ForEach-Object { echo "Processing $($_.BaseName)" Get-ChildItem -Path $_.FullName -Filter *.doc? | ForEach-Object { echo $_.FullName $document = $word_app.Documents.Open($_.FullName) $pdf_filename = "$($_.DirectoryName)\$($_.BaseName).pdf" $document.SaveAs([ref] $pdf_filename, [ref] 17) $document.Close() } } $word_app.Quit()
I hope this helps you with Powershell conversion lifestyle!