If you’re in the business of extracting data from disparate systems and presenting that in reports, then you’ll know how time-consuming (and frankly boring) it can be to format that data into tables or formats suitable for said report. Well, if your data is accessible via Powershell then this is the set of blogs for you! I’ll be writing a multi-part series on creating and manipulating Word documents in Powershell so that you can start to automate those tedious tasks and improve the consistency of your documents!


Table of Contents

  1. Opening the Word Document, Writing Some Text, Using Styles
  2. Adding and Manipulating Tables, Adding Images
  3. Document Properties, Table of Contents, Changing Page Properties
  4. Moving around the Document

Use a Template, or create everything on the fly?

It can be tempting, once you get to the end of this blog, to think “Ah great, now I can create an entire Word document directly in Powershell” and I get that completely - I had the same thought the first time I started working with this. However, there are two big reasons why I’d advise against it:

  1. It’s an awful lot of Powershell - adding all of the repeatable pieces of your document as part of the Powershell can make your script look very busy, and any modifications require you to pick through the Powershell and find the specific piece you need to update
  2. You end up repeating a lot of code! We should all be trying to reduce the amount of repeated code in our scripts; it’s inefficent, and making changes to one piece of code requires you to change all like pieces of code to maintain consistency.

Because of those reasons, I prefer to start with a templated file that sets the key components that are in every document created using the process - a title page, the table of contents, maybe (depending on the use case) text that is consistent regardless of the specifics in the document. Therefore, although I might add examples below on adding elements I include in templates the bulk will be assuming you’re also using a template.

Opening the Word document

Before we can do anything else, we need to open Word and the document we want to modify. All manipulation with word uses the Word.Application ComObject and so we don’t need to install or import any modules.

$word = New-Object -ComObject Word.Application

#If you're starting with a blank document
$doc = $word.Documents.Add()

#If you're starting with a templated document
$doc = $word.Documents.Open([Path to File])

TIP: If you want to see the changes that are being made to the document live, then just add this line as well:

$word.visible = $true

Setting the “Cursor”

Now that we’ve got our Word document and it’s open, we need to be able to start creating text inside it - and just like working with Word traditionally, we need to set our “cursor” location.

There are a number of different ways you can do this, depending on the complexity of your requirements and if you’re working with a template or not, but for the purpose of this we’re going to assume you want to start at the beginning of the document - and so we’re going to create our cursor at the default location:

$text = $word.Selection

Styles and Text

The $text object is going to be our most used on throughout this series, as we move our “cursor” through the document, and below you can see it in action:


$text.style = "Heading 1"
$text.TypeText("This is Heading 1")
$text.TypeParagraph()

$text.style = "Body Text"
$text.TypeText("This is a section of text immediately under Heading 1. This might be where you add some dynamically generated information")
$text.TypeParagraph()

$text.style = "Heading 2"
$text.TypeText("This is Heading 2")
$text.TypeParagraph()

There’s a couple of different things going on here, so let me explain each property:

  • $text.style sets the style of the current selection to one of your existing predefined styles; I’ve chosen to use the default ones you get in Word, but you may have your own pre-defined styles to use - if so, just use the name of them.
  • $text.TypeText does what it suggest it does - it types text! Just enter whatever text you want to be typed into the page here, and it will go ahead and do it.
  • $text.TypeParagraph is the same as pressing Enter or Return on your keyboard; it will start a new paragraph

Before I start typing text, I explicitly set the Style - this will depend on your specific styles and whatever the ‘Style to use next’ is set to, but I like to do this as a method of ensuring the document is styled exactly how I expect it to.

And that’s it! You’ll now have the beginnings of a Word document. In the next part where we tackle tables I’ll also show how you might use that to present information collected from an external source, such as an API. Below is a full script to achieve everything I’ve talked about in this post - make sure you keep a hold of this, as you’re going to be using it in the following parts:

$word = New-Object -ComObject Word.Application
$word.visible = $true

#If you're starting with a blank document
$doc = $word.Documents.Add()

$text = $word.Selection

$text.style = "Heading 1"
$text.TypeText("This is Heading 1")
$text.TypeParagraph()

$text.style = "Body Text"
$text.TypeText("This is a section of text immediately under Heading 1. This might be where you add some dynamically generated information")
$text.TypeParagraph()

$text.style = "Heading 2"
$text.TypeText("This is Heading 2")
$text.TypeParagraph()

$doc.SaveAs([Path To Save File To])
$doc.Close
$word.Quit