This PowerShell Module allows you to create Microsoft Word (.docx) document without Microsoft Word installed. That basically means you can create Word documents on servers, or shared workstations without Microsoft Office. It also means it can be run in background and doesn't require any COM objects.
PSWriteWord utilizes an open source module DocX (where commercial brother is called Xceed Words for .NET) that's available on GitHub. Originally this C# library was created by Cathal Coffey and later on maintained by me (Przemysław Kłys – LinkedIn). It was however in slow development so both Cathal and I decided to let the project go into more capable hands with support and further features getting added (slowly but each new version adds some stuff – commercial version is few versions ahead of the free one). Old C# Library (that I took a part in) is still available on GitHub under Classic Branch.
Well.. have you ever got a request for help with creating multiple word documents based on dynamic data? Maybe you want to create some simple Invoice or send a new agreement to all employees based on their AD data… well now you can. While this method was available for long time (and I've utilized it for many of my apps in C# / .NET) it is now possible to do in PowerShell. No compilation, no exe, should work on any newer Windows system (.NET Core is still not supported by module). Keep in mind that this is still work in progress…
Some of those commands are not yet finished and some are going to evolve. Some will get new features, some will be deprecated (or not really needed).
Below is one of the simplest examples. You define path to new file, you define new WordDocument and use special command Add-WordText which contains lots of possible options. You will be using this command a lot. Finally we save document.
Import-Module PSWriteWord #-Force $FilePath = "$Env:USERPROFILE\Desktop\PSWriteWord-Example-CreateWord1.docx" ### define new document $WordDocument = New-WordDocument $FilePath ### add 3 paragraphs Add-WordText -WordDocument $WordDocument -Text 'This is a text' -FontSize 10 Add-WordText -WordDocument $WordDocument -Text 'This is a text font size 21' -FontSize 21 Add-WordText -WordDocument $WordDocument -Text 'This is a text font size 15' -FontSize 15 ### Save document Save-WordDocument $WordDocument ### Start Word with file Invoke-Item $FilePath
Notice how it created 3 paragraphs in 1 second. Also notice how Word marked all text red because the language of the document was not set so it took defaults from my Word setup. Keep in mind it doesn't need Word installed. It doesn't open Microsoft Word in background. It works on the XML level.
Below example adds saving with language set for all text to ‘en-US' culture. It also adds some underlining and sets Heading Type.
Import-Module PSWriteWord #-Force $FilePath = "$Env:USERPROFILE\Desktop\PSWriteWord-Example-CreateWord4.docx" $WordDocument = New-WordDocument $FilePath Add-WordText -WordDocument $WordDocument -Text 'This is a text' -FontSize 10 -SpacingBefore 50 -UnderlineStyle singleLine -Supress $True Add-WordText -WordDocument $WordDocument -Text 'This is a text' -FontSize 10 -SpacingBefore 15 -Bold $true -Supress $True Add-WordText -WordDocument $WordDocument -Text 'This is a text with Heading type 3' -FontSize 15 -HeadingType Heading3 -FontFamily 'Arial' -Italic $true Save-WordDocument $WordDocument -Language 'en-US' ### Start Word with file Invoke-Item $FilePath
Another example shows how to create Pie Chart.
Import-Module PSWriteWord #-Force $FilePath = "$Env:USERPROFILE\Desktop\PSWriteWord-Example-CreateCharts1.docx" $WordDocument = New-WordDocument $FilePath Add-WordText -WordDocument $WordDocument -Text 'Pie Chart Example #1' ` -FontSize 15 ` -Color Blue ` -Bold $true -HeadingType Heading1 Add-WordPieChart -WordDocument $WordDocument -ChartName 'My finances' -Names 'Today', 'Yesterday', 'Two days ago' -Values 1050.50, 2000, 20000 -ChartLegendPosition Bottom -ChartLegendOverlay $false Add-WordText -WordDocument $WordDocument -Text 'Pie Chart Example #2' ` -FontSize 15 ` -Color Blue ` -Bold $true -HeadingType Heading1 Add-WordPieChart -WordDocument $WordDocument -ChartName 'My finances' -Names 'Today', 'Yesterday' -Values 2000, 20000 -ChartLegendPosition Left -ChartLegendOverlay $true Save-WordDocument $WordDocument -Language 'en-US' ### Start Word with file Invoke-Item $FilePath
As you can see 2 Charts are added to Word Document, language is set to ‘en-US' and 2 headings are created. All that with just few lines of code.
Before you are able to use this script you need to do few manual steps. Since this script is published as module… it's quite easy to set this up. Just execute command below (accept warnings) .. and start creating Microsoft Word document in PowerShell.
Install-Module PSWriteWord #Update-Module PSWriteWord
You can of course install everything manually from GitHub (as everything is published there) but it will be far easier to just use Install-Module.