Convert Word documents using PowerShell

Last updated: 2019-11-26

PowerShellDescription

This script converts Word compatible documents to a selected format utilizing the Word SaveAs function. Each file is converted by a single dedicated Word COM instance.

The script converts either all documents in a single folder matching an include-filter or a single file.

Currently supported target document types:

  • Default –> Word 2016
  • PDF
  • XPS
  • HTML

The conversion is handled by Word itself, utilizing the SaveAs method.

The Word.Document.SaveAs method supports the following wdFormat values:

NameValueDescription
wdFormatDocument0Microsoft Office Word 97 – 2003 binary file format.
wdFormatDocument970Microsoft Word 97 document format.
wdFormatTemplate1Word template format.
wdFormatTemplate971Word 97 template format.
wdFormatText2Microsoft Windows text format.
wdFormatTextLineBreaks3Windows text format with line breaks preserved.
wdFormatDOSText4Microsoft DOS text format.
wdFormatDOSTextLineBreaks5Microsoft DOS text with line breaks preserved.
wdFormatRTF6Rich text format (RTF).
wdFormatEncodedText7Encoded text format.
wdFormatUnicodeText7Unicode text format.
wdFormatHTML8Standard HTML format.
wdFormatWebArchive9Web archive format.
wdFormatFilteredHTML10Filtered HTML format.
wdFormatXML11Extensible Markup Language (XML) format.
wdFormatXMLDocument12XML document format.
wdFormatXMLDocumentMacroEnabled13XML document format with macros enabled.
wdFormatXMLTemplate14XML template format.
wdFormatXMLTemplateMacroEnabled15XML template format with macros enabled.
wdFormatDocumentDefault16Word default document file format. For Word, this is the DOCX format.
wdFormatPDF17PDF format.
wdFormatXPS18XPS format.
wdFormatFlatXML19Open XML file format saved as a single XML file.
wdFormatFlatXML20Open XML file format with macros enabled saved as a single XML file.
wdFormatFlatXMLTemplate21Open XML template format saved as a XML single file.
wdFormatFlatXMLTemplateMacroEnabled22Open XML template format with macros enabled saved as a single XML file.
wdFormatOpenDocumentText23OpenDocument Text format.
wdFormatStrictOpenXMLDocument24Strict Open XML document format.

Implemented document formats are shown in bold.

Examples

# EXAMPLE
# Convert all .doc files in E:\temp to Default
.\Convert-WordDocument.ps1 -SourcePath E:\Temp -IncludeFilter *.doc 

# EXAMPLE
# Convert all .doc files in E:\temp to XPS
.\Convert-WordDocument.ps1 -SourcePath E:\Temp -IncludeFilter *.doc -TargetFormat XPS

# EXAMPLE# Convert a single document to Word default format
.\Convert-WordDocument.ps1 -SourcePath E:\Temp\MyDocument.doc

Version History

  • 1.0, Initial community release
  • 1.1, Updated Word cleanup code

Links

Follow

Entdecke mehr von Granikos GmbH & Co. KG

Jetzt abonnieren, um weiterzulesen und auf das gesamte Archiv zuzugreifen.

Weiterlesen