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

{rtf1ansiansicpg1252deff0nouicompatdeflang1033{fonttbl{f0fnilfcharset0 Consolas;}}
{*generator Riched20 6.2.8102}viewkind4uc1
pardsa200sl276slmult1f0fs22lang9 Testtab Test tab Testpar
Testtab Test tab Testpar
Testtab Test tab Testpar
}

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='{rtf1ansiansicpg1252deff0nouicompatdeflang1033{fonttbl{f0fnilfcharset0 Consolas;}}{*generator Riched20 6.2.8102}viewkind4uc1 pardsa200sl276slmult1f0fs22lang9 ‘

$RTFFooter=’}’

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

ADD-CONTENT C:PowershellFilename.rtf -value $RTFHeader -force

ADD-CONTENT C:PowershellFilename.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:PowershellFilename.rtf -value $RTFHeader -force

ADD-CONTENT C:PowershellFilename.rtf -value “This is a line in the RTF documentpar”
ADD-CONTENT C:PowershellFilename.rtf -value “This is another linetab separated by a tabpar ”
ADD-CONTENT C:PowershellFilename.rtf -value “And one morepar”

ADD-CONTENT C:PowershellFilename.rtf -value $RTFFooter

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

Sean
The Energized Tech