Powershell: Convert Word documents to PDF

# This script uses Microsoft Word to convert all .doc and .docx files
# within a folder to PDF files.
# Note 1: Word runs invisibly in the background while the script is running.
# Note 2: This script will not overwrite existing PDF files.

# Set the folder where the .doc and .docx files are
$folder = 'c:\path\to\folder\'

# Create an instance of Word
$word = New-Object -ComObject Word.Application

# Find all .doc and .docx files in $folder and
# loop through them, saving them as PDF files.
Get-ChildItem -Path $folder -Filter *.doc? | ForEach-Object {

# Open the doc? file
$file = $word.Documents.Open($_.FullName)

# Change the file extension to .pdf
$pdf = "$($_.DirectoryName)\$($_.BaseName).pdf"

# Save and close the file
# If a PDF file with this name already exists it will NOT be overwritten.
$file.SaveAs([ref] $pdf, [ref] 17)

# Exit Word