Save Get-Help PureStoragePowerShell to PDF

I met with a customer this past week and they asked if we had a copy of all of our PowerShell help in Portable Document Format (PDF). Unfortunately at that moment we did not but now we do thanks to a little PowerShell and Microsoft Word.

The first step in the script is to setup a variable to define the output file of the contents from Get-Help.

$GetHelpOutputPath = "C:\temp\PureStoragePowerShell-Help.txt"

Next is to run the Get-Command for the PureStoragePowerShell module then iterate through that output to get full details for each cmdlet and pass the content to the previously defined output file.

Get-Command -Module PureStoragePowerShell | Foreach-Object { Get-Help $_.name -detailed } | Out-File $GetHelpOutputPath

Now begins creating the PDF file. First set a variable to use for the PDF output file name.

$GetHelpPDFPath = "C:\temp\Pure Storage PowerShell Toolkit Help.pdf"

We can open up the output file from the beginning in Word and just use Word’s feature to save a PDF file. An important detail is that the system this will be run on requires that Microsoft Word be installed.

$word = New-Object -ComObject word.application
$word.visible = $false
$pdf = $word.Documents.Open($GetHelpOutputPath)

When creating the new Word COM object we set it to be invisible, just change that to $true and it will show Word while the script runs. All that is happening here is that the output file is being opened in Microsoft Word.

The basic output from the Get-Help command does not have any specific title information so with the next few lines of script we create a basic title to be added to the document before it is saved as a PDF.

$selection = $word.Selection
$selection.typeText("Pure Storage PowerShell Toolkit (2.6.0.401) Get-Help")
$selection.TypeParagraph()

Finally we save the file and clean-up close the document (pdf) and quitting Word. There was a variable set earlier in the script $wdExportFormatPDF = 17 which defines that PDF is the format.

$pdf.saveas([ref] $GetHelpPDFPath, [ref] $wdExportFormatPDF)  
$pdf.close()
$word.Quit()

One thing to be careful of when testing this script is that if a new Word COM object is created and the $word.Quit() is never run there will be Microsoft Word processes running on the system just not visible. You can check this using CTRL+SHIFT+ESC to access the Task Manager.

Below is the entire script:

$GetHelpOutputPath = "C:\temp\PureStoragePowerShell-Help.txt"
Get-Command -Module PureStoragePowerShell | Foreach-Object { Get-Help $_.name -detailed } | Out-File $GetHelpOutputPath
$GetHelpPDFPath = "C:\temp\Pure Storage PowerShell Toolkit Help.pdf"
$wdExportFormatPDF = 17
$word = New-Object -ComObject word.application
$word.visible = $false
$pdf = $word.Documents.Open($GetHelpOutputPath)
$selection = $word.Selection
$selection.typeText("Pure Storage PowerShell Toolkit (2.6.0.401) Get-Help")
$selection.TypeParagraph()
$pdf.saveas([ref] $GetHelpPDFPath, [ref] $wdExportFormatPDF)  
$pdf.close()
$word.Quit()

Thanks Chris N. for the great idea!

Cheers,
barkz