This PowerShell script sample shows how to convert Word document to PDF file.
Converting Word document to PDF file is a frequently asked question in many public forums.
Step 1: Click Start, type powershell in the search box on the Start Menu, right-click the Windows PowerShell icon, and then click Run Windows PowerShell as administrator. When the User Account Control dialog box appears, confirm that the action displayed is correct, Then, click Continue.
Step 2: Run the script in the Windows PowerShell Console, type the command: Import-Module <Script Path>. For example, type Import-Module C:\Script\ConvertPowerPointToWordDocument.psm1
This is shown in the following figure.

Try
{
#Displays a progress bar within a Windows PowerShell command window.
Write-Progress -Activity "Converting Word document [$wdDocumentName] to PDF file" `
-Status "$intNumberwd of $wdDocumentCounts Word document(s)" -PercentComplete $($intNumberwd/$wdDocumentCounts*100)
#Open the Word document
$wdDocument = $wdApplication.Documents.Open($wdDocumentPath)
#Sets the format paramters
$wdDocument.ExportAsFixedFormat($OutputFilePath,$wdExportFormat,$OpenAfterExport,`
$wdExportOptimizeFor,$wdExportRange,$wdStartPage,$wdEndPage,$wdExportItem,$IncludeDocProps,`
$KeepIRM,$wdExportCreateBookmarks,$DocStructureTags,$BitmapMissingFonts,$UseISO19005_1)
#release the object
$wdDocument.Close()
[void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($wdDocument)
$Properties = @{'File Name' = $wdDocumentName
'Action(Convert to PDF)' = If(Test-Path -Path $OutputFilePath)
{"Finished"}
Else
{"Unfinished"}
}
$objWord = New-Object -TypeName PSObject -Property $Properties
$objWord
}
Catch
{
Write-Warning "A few Word documents have been lost in this converting. $wdDocumentName cannot convert to PDF file."
}
Try { #Displays a progress bar within a Windows PowerShell command window. Write-Progress -Activity "Converting Word document [$wdDocumentName] to PDF file" ` -Status "$intNumberwd of $wdDocumentCounts Word document(s)" -PercentComplete $($intNumberwd/$wdDocumentCounts*100) #Open the Word document $wdDocument = $wdApplication.Documents.Open($wdDocumentPath) #Sets the format paramters $wdDocument.ExportAsFixedFormat($OutputFilePath,$wdExportFormat,$OpenAfterExport,` $wdExportOptimizeFor,$wdExportRange,$wdStartPage,$wdEndPage,$wdExportItem,$IncludeDocProps,` $KeepIRM,$wdExportCreateBookmarks,$DocStructureTags,$BitmapMissingFonts,$UseISO19005_1) #release the object $wdDocument.Close() [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject($wdDocument) $Properties = @{'File Name' = $wdDocumentName 'Action(Convert to PDF)' = If(Test-Path -Path $OutputFilePath) {"Finished"} Else {"Unfinished"}} $objWord = New-Object -TypeName PSObject -Property $Properties $objWord } Catch { Write-Warning "A few Word documents have been lost in this converting. $wdDocumentName cannot convert to PDF file."}
Example 1: Type ConvertTo-OSCPDF –Path D:\Word in the Windows PowerShell Console.
This command will convert all Word documents to PDF files.


Example 2: Type ConvertTo-OSCPDF –Path D:\Word\ -StartPage 1 –EndPage 10 in the Windows PowerShell Console.
This command will convert from page 1 to page 10 of a Word document to a PDF file.

