Powershell With MS Word

Create a Word document


Create a Word document from PowerShell. The script uses the COM Office including Word of the components, it is therefore necessary that MS-Office is installed on the computer that runs the script.

In this example we will create a document with a title style "Heading 1" body text with the font "Calibri" size 10 and page numbers in the footer. That being said, it looks like Calibri is making its way into more and more places... even before the release of Office 2007. What if you don't like Calibri? You can simply change the font "Times New Roman" The content of the document is the list of processes running.

Example

    
$word = new-object -com word.application
$doc = $word.documents.add()
$pn = $doc.sections.item(1).footers.item(3).pagenumbers.add()
$doc.content.text = "Add Some Text here"
$doc.content.paragraphs.item(1).style = $doc.styles.item("Title 2")
$doc.content.insertparagraphafter()
$doc.Paragraphs.item(2).range.font.name = "Calibri"
$doc.Paragraphs.item(2).range.font.size = 10
$doc.Paragraphs.item(2).range.text = (get-process | out-string)
$doc.saveas("C:\test\Test.doc")
$doc.close()
    

Copy and Try it



Result word document created