I needed a demonstration for one of my last online PowerShell classes on using COM objects in PowerShell. I took an old VBScript that used Microsoft Word to get document statistics such as word and page count and transformed it into PowerShell. I quickly realized I could flesh out the demo into a larger function that I can actually use. Given that I generate a lot of Word docs, being able to get some document statistics is helpful. So I created a function called Get-DocStatistic. Here’s the function:
The function uses Begin, Process and End scriptblocks so you can pipe objects to it. In the Begin script block I create the Word.Application COM object. I only need to do this once so using the Begin script block is perfect because any code here executes once before any pipelined objects are processed.
In the Process script block I create new object for the Word document. This is also a COM object. You can pipe it to Get-Member to discover all of its properties and methods. One if its methods is ComputeStatistics(). This method takes a parameter indicating what statistic to compute. In VBScript I would have had to define a constant value for something like wdStatisticWords. But in PowerShell all I have to do is use the constant. PowerShell will get the correct constant value.
Throughout the Process block I use Write-Progress to let me know what is happening. I have a lot of Word docs and piping them to Get-Docstatistic can take some time.
After all the objects are processed the End script block runs where I quit and close Microsoft Word. Without this command I would have a Winword process running in the background.
Here are some examples on how you might use it.
PS C:\> dir c:\test\dns.docx | Get-DocStatistic
PS C:\test> dir c:\test\dns.docx | get-docstatistic
Document : C:\test\dns.docx
Directory : C:\test
Filename : dns.docx
Size : 23606
Words : 1952
Lines : 510
Paragraphs : 422
Pages : 10
PS C:\> $stats=dir “$env:userprofile\documents\*” -include *.doc,*.docx -recurse| Get-DocStatistic
PS C:\> $stats | Measure-Object pages -Sum
PS C:\scripts\PoSH> $stats | Measure-Object pages -Sum
Count : 319
Sum : 7615
Property : Pages
I don’t know if you’ll find as much a need for this as I do, but you hopefully you’ve see how easy it is to use COM objects in PowerShell.