Piping Results Straight Into Office Word

by Nov 23, 2015

With only a few lines of code you can implement a command called Out-OfficeWord. It takes the data you pipe into it and inserts them into a new Word document (provided Word is installed).

#requires -Version 1

function Out-OfficeWord
{
  param
  (
    $Font = 'Courier',
    
    $FontSize = 12,
    
    $Width = 80,
    
    [switch]
    $Landscape
  )

  $Text = $Input | Out-String -Width $Width
  
  $WordObj = New-Object -ComObject Word.Application
  $document = $WordObj.Documents.Add()
  $document.PageSetup.Orientation = [Int][bool]$Landscape
  $document.Content.Text = $Text
  $document.Content.Font.Size = $FontSize
  $document.Content.Font.Name = $Font
  $document.Paragraphs | ForEach-Object { $_.SpaceAfter = 0 }
  $WordObj.Visible = $true
}

To create a list of running processes in Word, simply run this:

Get-Process | Out-OfficeWord -Landscape -Font Consolas -FontSize 8

From here, you can save the results as PDF, enhance formatting, or print them.

Twitter This Tip! ReTweet this Tip!