Now we continue forward with leveraging the ability of having Windows PowerShell build out an RTF files without the need for Microsoft Word. 

Just think.  You can NOW generate reports on your server that can be read my Managers and Directors with NO pre-requisite for any software OTHER than Windows PowerShell.

Now one of the things I decided to play with was generating content that’s typically only available in Microsoft Word into a generic RTF file.  Something like a Table.

If you look at the menu in Wordpad / Write you will see NOWHERE an option to “insert table”

Just look.


But we DO know that if you were to generate a simple blank table in Microsoft Word, copy and paste it into Wordpad, it will render just fine as can be seen below with our simple 3×3 table.


Now we just open up this document to find what a SINGLE row looks like.  We’ll use Notepad again but look for the words ‘Patch and Switch’ to see what the formatting looks like for that row.


We will then immediately ‘Permanently Borrow’ that code for out PowerShell script Smile


Now since really this my first crack at doing this, I decided to create an additional function for the columns since they will have multiple values.  (You know as I’m typing this, I’m already thinking of a few ways to redo it too….)

The Write-RTF column function will be in a simliar format.   It will output a Single line each time but accept three or four values (Hint: I cheated and ‘Permanently Borrowed’ the code for Four column output as well)

So as before we’ll have a string which contains the same content as the RTF code from the text file, but we’ll swap out the words like ‘Patch and Switch’, ‘Technet’ and ‘Big Guy’ with actual Powershell variables.

Function Write-RTFColumn
        # Regular Line – 3 column output
        ‘0’ { $output=”\trowd\trgaph108\trleft5\trbrdrl\brdrs\brdrw10 \trbrdrt\brdrs\brdrw10 \trbrdrr\brdrs\brdrw10 \trbrdrb\brdrs\brdrw10 \trpaddl108\trpaddr108\trpaddfl3\trpaddfr3
\clbrdrl\brdrw10\brdrs\clbrdrt\brdrw10\brdrs\clbrdrr\brdrw10\brdrs\clbrdrb\brdrw10\brdrs \cellx3121\clbrdrl\brdrw10\brdrs\clbrdrt\brdrw10\brdrs\clbrdrr\brdrw10\brdrs\clbrdrb\brdrw10\brdrs \cellx6238\clbrdrl\brdrw10\brdrs\clbrdrt\brdrw10\brdrs\clbrdrr\brdrw10\brdrs\clbrdrb\brdrw10\brdrs \cellx9355
\pard\intbl\widctlpar\f1\fs22 “+$value1+”\cell “+$value2+”\cell “+$value3+”\cell\row
\pard\sa200\sl276\slmult1\f2\lang9`r`n” }
    Write-RTFDoc $output



If you look you’ll see I’ve highlighted where I’ve put the values in with Bold and Underscore.   If you compare the original code from the RTF file here are the values that were there originally.

\clbrdrl\brdrw10\brdrs\clbrdrt\brdrw10\brdrs\clbrdrr\brdrw10\brdrs\clbrdrb\brdrw10\brdrs \cellx3121\clbrdrl\brdrw10\brdrs\clbrdrt\brdrw10\brdrs\clbrdrr\brdrw10\brdrs\clbrdrb\brdrw10\brdrs \cellx6238\clbrdrl\brdrw10\brdrs\clbrdrt\brdrw10\brdrs\clbrdrr\brdrw10\brdrs\clbrdrb\brdrw10\brdrs \cellx9355
\pard\intbl\widctlpar\f1\lang1033 Technet\f2\lang9\cell\f1\lang1033 Big Guy\f2\lang9\cell\f1\lang1033 Patch and Switch\f2\lang9\cell\row


If you’re like my sample script so you can play for yourself without cutting and scraping screen bits together I’ve uploaded it to the Technet Script Gallery for all to share.  

It’s a pretty simple idea and it’s DEFINITELY something that can be expanded upon (especially in the processes).  

Other ways it could be improved upon would be options to select fonts and Pitch, Bold, Italics or even allowing data to be piped to a function.

Let’s just this “V1”, download and play with it.  At the least you can use it now to get data from a server and build an RTF file directly with it and use “SEND-MailMessage” to email those lovely reports!

Enjoy ! If you have any questions you can ping me anytime.  This was a fun one to pull together.

The Energized Tech