Script to convert Word document to PDF file (PowerShell)

Introduction

This PowerShell script sample shows how to convert Word document to PDF file.

Scenarios

Converting Word document to PDF file is a frequently asked question in many public forums.

Script

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.

Step 3: Type the following command to display the Help file for this function, such as the syntax, parameters, or examples: Get-Help ConvertTo-OSCPDF -Full
 
 
Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.
PowerShell
Edit|Remove
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

Example 1: Type ConvertTo-OSCPDF –Path D:\Word in the Windows PowerShell Console.

This command will convert all Word documents to PDF files.

The result is as follows:

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.

The result is as follows:
 
 

Prerequisite

Windows PowerShell 2.0
Word 2010 or higher version

Additional Resources

New-Object