Here’s an interesting idea I have been playing with. Creating an RTF document within Powershell.

Why RTF? RTF is “Rich Text Format”. If you haven’t ever looked at the format of one, the file content is actually 100% ASCII. Even though an RTF when presented through Word holds fonts and formatting it is actually a pure ASCII file.

Nice! So it would be possible to generate files with nice fonts and formatting. Sure we could do it with Dcom and Microsoft Word but the advantage to an RTF is it could be generated as a report on a server with NO additional software, no changes to the system

So here’s a sample RTF file using a Consolas 11 point font with a simple one line of ‘Test’

Test Test Test
Test Test Test
Test Test Test

Viewing the document in the natural RTF code it appears like this

{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Consolas;}}
{\*\generator Riched20 6.2.8102}\viewkind4\uc1
\pard\sa200\sl276\slmult1\f0\fs22\lang9 Test\tab Test \tab Test\par
Test\tab Test \tab Test\par
Test\tab Test \tab Test\par

Online Microsoft has a large document called “Word 2007: Rich Text Format (RTF) Specification, version 1.9.1” which details all the nuances of this language. Believe me, it IS a language.

Really for a very basis RTF file we need a header, some content and the termination at the end. Looking in the document we can find out there are three key types of data within the document without getting too far into the mud. Control words, Control Symbols and Groups.

If you REALLY want to have some deep reading, dig into the document. But for today we’ll show you all you need to make a Basic RTF Document.

Looking at this particular document we can see the words Test separated by \tab and ending with a \par for each line. Really this isn’t much of a guess to figure out that \par is the ending of a paragraph and \tab are fields separated by Tabs.

So let’s think, if this is pure text, all we need to do to create a Basic RTF is to grab everything BEFORE our content and looking at the bottom terminate it with a Parentheses }

$RTFHeader='{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Consolas;}}{\*\generator Riched20 6.2.8102}\viewkind4\uc1 \pard\sa200\sl276\slmult1\f0\fs22\lang9 ‘


So a basic RTF document could be created using a very simple ADD-CONTENT like this

ADD-CONTENT C:\Powershell\Filename.rtf -value $RTFHeader -force

ADD-CONTENT C:\Powershell\Filename.rtf -value $RTFFooter

There that’s pretty simple. Now that means all we need is place text in the middle and end each line with a ‘\par ‘ for a basic line.

ADD-CONTENT C:\Powershell\Filename.rtf -value $RTFHeader -force

ADD-CONTENT C:\Powershell\Filename.rtf -value “This is a line in the RTF document\par”
ADD-CONTENT C:\Powershell\Filename.rtf -value “This is another line\tab separated by a tab\par ”
ADD-CONTENT C:\Powershell\Filename.rtf -value “And one more\par”

ADD-CONTENT C:\Powershell\Filename.rtf -value $RTFFooter

Nice! A Simple RTF file. Of course we can do more, and we’ll play a bit tomorrow Smile

The Energized Tech