PowerShell

PSWriteWord – Updated to 0.4.1 (Breaking Change included)

While the update may seem small – change from 0.4 to 0.4.1 it actually brought a number of fixes and new features. One of the early adopters found some issues with how Add-WordTable worked so I've spent a lot of time fixing different cases on how people may use it. During that time I've created a bunch of different examples covering possible use cases. While with PSWriteWord you can build your table by hand… it has quite nice functionality that I think is very useful in your day to day scenario. To add few more features I had to change old -Table to -DataTable and use -Table parameter for something else. So if you've managed to use the old version… you may need to change just one bit.

PSWriteWord Information
Please notice this article contains parts of information (still useful) and may not reflect all functionalities of this module. For download, source code and so on you should refer to the dedicated PSWriteWord module page. After reading this one… of course! It contains useful informationexamples and know-how.

Let's take a look at few examples… shall we?

How can I use it?

Below example is simple use case. Trying to build an invoice table with 2 columns Description and Amount. Normally you would have to go thru adding rows and columns looping thru, adding alignments, design and so on.  While you can still do that… you don't have to.

Import-Module PSWriteWord #-Force

$FilePath = "$Env:USERPROFILE\Desktop\PSWriteWord-Example-Tables3.docx"

#Clear-Host
$WordDocument = New-WordDocument $FilePath

$InvoiceEntry1 = @{}
$InvoiceEntry1.Description = 'IT Services 1'
$InvoiceEntry1.Amount = '$200'

$InvoiceEntry2 = @{}
$InvoiceEntry2.Description = 'IT Services 2'
$InvoiceEntry2.Amount = '$300'

$InvoiceEntry3 = @{}
$InvoiceEntry3.Description = 'IT Services 3'
$InvoiceEntry3.Amount = '$288'

$InvoiceEntry4 = @{}
$InvoiceEntry4.Description = 'IT Services 4'
$InvoiceEntry4.Amount = '$301'

$InvoiceEntry5 = @{}
$InvoiceEntry5.Description = 'IT Services 5'
$InvoiceEntry5.Amount = '$299'

$InvoiceData = @()
$InvoiceData += $InvoiceEntry1
$InvoiceData += $InvoiceEntry2
$InvoiceData += $InvoiceEntry3
$InvoiceData += $InvoiceEntry4
$InvoiceData += $InvoiceEntry5

Add-WordText -WordDocument $WordDocument -Text "Invoice Data" -FontSize 15 -Alignment center
Add-WordParagraph -WordDocument $WordDocument
Add-WordTable -WordDocument $WordDocument -DataTable $InvoiceData -Design LightShading

Save-WordDocument $WordDocument

### Start Word with file
Invoke-Item $FilePath

Got anything more?

Another example shows couple of different Add-WordTable examples. It covers 3 cases. Hash, Ordered Hash and Array of Sytem.Objects. All 3 are supported by Add-WordTable. Notice how you need to have [ordered] for hash to get proper order of values.  Also notice how column names are given by parameter (or by default Name/Value).

### prepare data
$hash = [ordered] @{}
$hash.add("HQ-1", "5.54.546")
$hash.add("EUR-1", "6.0.0.1")
$hash.add("HQ-2", "5.6")
$hash.add("EUR-2", "6.1.5")
$hash.add("EUR-3", "6.2")

$hash1 = @{}
$hash1.add("HQ-1", "5.54.546")
$hash1.add("EUR-1", "6.0.0.1")
$hash1.add("HQ-2", "5.6")
$hash1.add("EUR-2", "6.1.5")
$hash1.add("EUR-3", "6.2")

$hash2 = @{}
$hash2.add("HQ-1", "5.54.546")

$obj = New-Object System.Object
$obj | Add-Member -type NoteProperty -name Name -Value "Ryan_PC"
$obj | Add-Member -type NoteProperty -name Manufacturer -Value "Dell"
$obj | Add-Member -type NoteProperty -name ProcessorSpeed -Value "3 Ghz"
$obj | Add-Member -type NoteProperty -name Memory -Value "6 GB"

$myObject2 = New-Object System.Object
$myObject2 | Add-Member -type NoteProperty -name Name -Value "Doug_PC"
$myObject2 | Add-Member -type NoteProperty -name Manufacturer -Value "HP"
$myObject2 | Add-Member -type NoteProperty -name ProcessorSpeed -Value "2.6 Ghz"
$myObject2 | Add-Member -type NoteProperty -name Memory -Value "4 GB"

$myObject3 = New-Object System.Object
$myObject3 | Add-Member -type NoteProperty -name Name -Value "Julie_PC"
$myObject3 | Add-Member -type NoteProperty -name Manufacturer -Value "Compaq"
$myObject3 | Add-Member -type NoteProperty -name ProcessorSpeed -Value "2.0 Ghz"
$myObject3 | Add-Member -type NoteProperty -name Memory -Value "2.5 GB"

$myArray = @($obj, $myobject2, $myObject3)
### prepare data end

Import-Module PSWriteWord #-Force
$FilePath = "$Env:USERPROFILE\Desktop\PSWriteWord-Example-Tables6.docx"

$WordDocument = New-WordDocument $FilePath
Add-WordTable -WordDocument $WordDocument -DataTable $hash -Design ColorfulList #-Verbose
Add-WordParagraph -WordDocument $WordDocument

Add-WordTable -WordDocument $WordDocument -DataTable $hash -Design ColorfulGrid -Columns 'My Name', 'My Value'
Add-WordParagraph -WordDocument $WordDocument

Add-WordTable -WordDocument $WordDocument -DataTable $hash1 -Design ColorfulGrid -Columns 'My Name', 'My Value' -AutoFit Window
Add-WordParagraph -WordDocument $WordDocument

Add-WordTable -WordDocument $WordDocument -DataTable $hash2 -Design ColorfulGrid -Columns 'My Name', 'My Value'
Add-WordParagraph -WordDocument $WordDocument

Add-WordTable -WordDocument $WordDocument -DataTable $myArray -Design ColorfulList #-Verbose

Save-WordDocument $WordDocument -Language 'en-US'
Invoke-Item $FilePath

PSWriteWord Information
Please notice this article contains parts of information (still useful) and may not reflect all functionalities of this module. For download, source code and so on you should refer to the dedicated PSWriteWord module page. After reading this one… of course! It contains useful informationexamples and know-how.

There are far more changes that has been added with this release but since it's a fast update… well you will have to find them out by yourself for now!

This post was last modified on June 28, 2018 22:16

Przemyslaw Klys

System Architect with over 14 years of experience in the IT field. Skilled, among others, in Active Directory, Microsoft Exchange and Office 365. Profoundly interested in PowerShell. Software geek.

Share
Published by
Przemyslaw Klys

Recent Posts

Active Directory Replication Summary to your Email or Microsoft Teams

Active Directory replication is a critical process that ensures the consistent and up-to-date state of…

2 weeks ago

Syncing Global Address List (GAL) to personal contacts and between Office 365 tenants with PowerShell

Hey there! Today, I wanted to introduce you to one of the small but excellent…

5 months ago

Active Directory Health Check using Microsoft Entra Connect Health Service

Active Directory (AD) is crucial in managing identities and resources within an organization. Ensuring its…

7 months ago

Seamless HTML Report Creation: Harness the Power of Markdown with PSWriteHTML PowerShell Module

In today's digital age, the ability to create compelling and informative HTML reports and documents…

8 months ago

How to Efficiently Remove Comments from Your PowerShell Script

As part of my daily development, I create lots of code that I subsequently comment…

9 months ago

Unlocking PowerShell Magic: Different Approach to Creating ‘Empty’ PSCustomObjects

Today I saw an article from Christian Ritter, "PowerShell: Creating an "empty" PSCustomObject" on X…

9 months ago