How to Create a Word Document with PowerShell

Using a PowerShell Script to Automate Microsoft Word

© Mark Alexander Bain

Jun 5, 2009
How to Create a Word Document with PowerShell, Mark Alexander Bain
These days there's no need for anyone to spend hours typing up those weekly reports. All they need is Microsoft Word and a PowerShell Script

Microsoft PowerShell provides a programmer with a simple yet effective way of automating many tasks on a user's computer. For example, if Microsoft Word is installed on the PC then the developer can quickly and easily write a script that will:

  • run Microsoft Word
  • create a new Microsoft Word document
  • add and format paragraphs
  • save the document
  • close Microsoft Word

This process can also be done invisibly so that the user does not see the file being written to on the screen. They just have to run the script and a new file will magically appear on their computer.

Running Microsoft Word with PowerShell

Microsoft Word is, of course, not a .NET framework application but PowerShell is. However that does not stop PowerShell from easily interacting with the Word application:

$oWord = New-Object -Com Word.Application
$oWord.Visible = $true

If this code is saved as a script (for example c:\powershell\word.ps1) and called form the command prompt (as shown in figure 1) then Microsoft Word will start. However, at the moment it will not contain any documents.

Creating a Microsoft Word Document with PowerShell

When a document is created it expects a number of parameters. However, the programmer does not need to know what these are. Instead they create a variable to be a place holder:

$oMissing = [System.Reflection.Missing]::Value

This “missing value” can then be used by the programmer to create the document:

$oDoc = $oWord.Documents.Add($oMissing, $oMissing, $oMissing, $oMissing)

If a user runs the script now then a blank document will appear.

Adding Paragraphs to a Microsoft Word Document with PowerShell

The programmer can add content to a document by creating and formatting paragraphs:

$oPara1 = $oDoc.Paragraphs.Add ($oMissing)
$oPara1.Range.Style = "Heading 1"
$oPara1.Range.Text = "Insert a Paragraph with PowerShell"
$oPara1.Range.InsertParagraphAfter()

The above example creates a heading whilst the next creates a paragraph that uses the default page style:

$oPara2 = $oDoc.Paragraphs.Add($oMissing)
$oPara2.Range.Text = "Microsoft Word automation is simple with PowerShell"
$oPara2.Range.InsertParagraphAfter()

The “InsertParagraphAfter“ function ensures that the paragraphs are added in order, one after the other.

Saving a Microsoft Word Document with PowerShell

If the user runs the script at this point (as shown in figure 2) then they will be presented with a formatted Word document. However, it does not net exist as a file on the computer. The next step, therefore, is to save the document:

$filename = 'C:\Reports\PowerShell.doc'
$oDoc.SaveAs($filename,
$oMissing, $oMissing,
$oMissing, $oMissing,
$oMissing, $oMissing,
$oMissing, $oMissing,
$oMissing, $oMissing)

This will save the document to the PC (and if the file exists already then it will be overwritten).

Closing a Microsoft Word Document with PowerShell

Once the script has added all of the required content the document should be closed:

$oDoc.Close()
$oWord.Quit()

At the end of the process the Word application will be closed leaving just the new document on the user's computer.

Further Reading

How to Create a Microsoft Word Document with C#


The copyright of the article How to Create a Word Document with PowerShell in Command Line Programming is owned by Mark Alexander Bain. Permission to republish How to Create a Word Document with PowerShell in print or online must be granted by the author in writing.


How to Create a Word Document with PowerShell, Mark Alexander Bain
Figure 1: Running a PowerShell Script, Mark Alexander Bain
Figure 2: Automating a Word Document, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo