Created
May 4, 2019 21:01
-
-
Save cl0ne/3555a007bd24e5aa80dbc15b29c772fa to your computer and use it in GitHub Desktop.
Small PowerShell snippet for converting .doc(x) files to PDF, tested with Word 2010
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Word2PDF { | |
$src_path = $args[0] | |
if($src_path -eq $null){ | |
Write-Host "Usage: Word2PDF source-files-dir" | |
return | |
} | |
if(!(Test-Path $src_path -PathType Container)){ | |
throw "Path $src_path doesn't exist or is not a directory." | |
} | |
$word_app = New-Object -ComObject Word.Application | |
Get-ChildItem -Path $src_path -Filter *.doc? | ForEach-Object { | |
Write-Host "Converting $_" | |
$document = $word_app.Documents.Open($_.FullName) | |
$pdf_filename = "$($_.DirectoryName)\$($_.BaseName).pdf" | |
$document.SaveAs([ref] $pdf_filename, [ref] 17) | |
$document.Close() | |
} | |
$word_app.Quit() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment