Skip to content

Instantly share code, notes, and snippets.

@aymenjaz
Created June 16, 2022 03:41
Show Gist options
  • Save aymenjaz/cafebc25c3a1bdb374633a1e2304e088 to your computer and use it in GitHub Desktop.
Save aymenjaz/cafebc25c3a1bdb374633a1e2304e088 to your computer and use it in GitHub Desktop.
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.
function Convert-WordToPDF
{
<#
.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
#>
[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()
}
# calling function
Convert-WordToPDF -SourceFolder C:\temp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment