Friday, 22 February 2019

Word documentation using PowerShell

Powershell With MS Word

Recently I got a request from one of my friend for automating the word document editing which was very interesting for me. When we need to create a word reports on a regular basis, we can automate this to avoid spending more time on documentation, sometimes it’s boring to do the same reports every day by changing a few items in the documents (It’s my friend’s comment), also we may miss to change something like date, name etc. in the document which is very important. We can automate documentation when we pull the reports from either a server, system or from a database using PowerShell and insert the report into the word document to have an automated word documenting system. We can also trigger an email from PowerShell by attaching this word documents which means reports are fully automated 
We are using com abject to interact with office application from PowerShell.  Below are the com object for office application and we are using the word com abject to interact with word using PowerShell.

Word.Application
Excel.Application
powerpoint.application
Outlook.Application

Please refer the below link to understand more about office Object model.  Before we start, make sure that we have installed MS office application on the machine
New-Object -ComObject Word.Application
I have highlighted the important properties in the object, please have a look.


























As soon as you executed the cmdlet, you can see a word process in the task manager, please go to task manager and check if you got a word process (Close all the other word document you opened before executing the cmdlet to make sure that that word is executed from PowerShell)




























You can also check the process status using PowerShell as shown below.










We need to enable the word visibility if you need to see the word document on the screen. I have highlighted the “Visible” property in the screenshot for your reference. To enable the visibility, follow the below cmdlet, this cmdlet is same for all the office applications.

$Word.Visible = $True

Once you execute the cmdlet you can see the Microsoft Word pup up with a blank page

Let’s start - I am printing a simple asset inventory report in the word document, this is a small example which shows how to create, format and save the word document using PowerShell

$Word = New-Object -ComObject word.application $Word.Visible = $true
$Docs = $Word.Documents.Add()
$Selection = $Word.Selection

Below are the few formatting which I have done, you can see the word color code from the below link, please note that some of  the formattings do not work if you are not using the latest PowerShell version

$Selection.Style = 'Heading 1'
$Selection.Font.Bold = 1
$Selection.Font.Color = '26367'

After the formatting, we can start adding the information into the word document. The information can be stored in a PowerShell variable after collecting from preferred sources, I am collecting a few system information using a WMI query and passing the information to a word document. Below is the information I am collecting for our word document.

$Sysdetails = Get-WmiObject -Class Win32_computersystem  | select -ExpandProperty TotalPhysicalMemory
$Osname = Get-WmiObject -Class Win32_OperatingSystem | select -ExpandProperty caption

I have collected the machine's operating system name and total physical memory available on the machine Follow the below command to write the collected information in a word document, after executing the command you can see the information is appearing in the word document, I have used a TypeParagraph() command which will print the details in the next line

$Selection.TypeText("Server Inventory Report for $(get-date) ")
$Selection.TypeParagraph()
$Selection.TypeParagraph()
$Selection.TypeText("Operating System Name = $osname")
$Selection.TypeParagraph()
$Selection.TypeText("Physical Memory = $sysdetails")

Finally, we got the below word document created using PowerShell 😊 

























We need to save the document now, follow the below command to save the word document. $WordPath = "D:\Temp\TestDocument.docx"
$Docs.SaveAs([ref]$WordPath,[ref]$SaveFormat::wdFormatDocument)
$Word.Quit()

I just covered a very basic topic in this session for you to start using the PowerShell to create a word document, please explore more about adding content, formatting, editing etc. I am planning to post another session with more details on creating a word document using PowerShell.
Click here to download the script