Now we return to the table and gaze back at what we’ve done.

  • Seen how Windows PowerShell can create a very simple RTF file with ADD-Content and some pre-defined strings
  • Created a simple function to allow us to easily swap out file names and send content to a file

 

From this point we’re going to step forward and make things a bit more interesting.  From the first article you saw I needed to send basically three things to make an RTF file happen.

  • A Header string (which defines default font, spacing etc)
  • Some content (our default ending in ‘par ‘ for each line)
  • A footer string (which is really just a ‘}’ )

Since each and every time we do this we’re just sending string content we can leverage yesterday’s function and some predefined strings for this in the following manner.

$Header=”{rtf1ansiansicpg1252deff0nouicompatdeflang1033{fonttbl{f0fnilfcharset0 Calibri;}{f1fnilfcharset2 Symbol;}}`r`n{*generator Riched20 6.3.9600}viewkind4uc1 “

Write-RTFDoc $Header

$Line1=’Hello Worldpar’

Write-RTFDoc $Line1

$Line2=’How’s it Going?par’

Write-RTFDoc $Line2

$Footer=”}”

Write-RTFDoc $Footer

Now this works but it’s not TOO different from our original solution.   Let’s take a step back and consider what we’re REALLY doing.   We’re sending text in some type of pre defined format.  

If you THINK about it I’m sending a definition of the output and either No actual visual output (Like the header and footer) or Visual output (such as our line of text).

Then what I can do is define a different function to accept simple content and then define the type of output for it.   Maybe just with a Switch statement in PowerShell?

Let’s create that function then to accept simply a number to define the format and a string (or lack of) for the content depending on what we’re sending.

Function Write-RTFLine

{

param ($Style,$Content)

switch ($Style)

     {

     # Header required for beginning of RTF file
     0′ { $output="{rtf1ansiansicpg1252deff0nouicompatdeflang1033     {fonttbl{f0fnilfcharset0 Calibri;}{f1fnilfcharset2 Symbol;}}`r`n{*generator Riched20 6.3.9600}viewkind4uc1 "}

     # Regular Line – Default font
     ‘1’ { $output=$Content+"par"}

     # Final Line required to close RTF output
     ’99’ { $output="}" }       
     }

}

 

Now we have a more interesting way to pass content to the RTF file.  We are only defining the type of output and the actual content.  To write to the file now we just pass it like this.

$Line1=’Something to write’

$Line2=’Something else to write’

Write-RTFLine 0 ; # Header

Write-RTFLine 1 $Line1

Write-RTFLine 1 $Line2

Write-RTFLine 99 # Footer

At this point start getting the brain spinning a bit.   Now that we’ve effectively separated the content from the formatting, what if we were to figure out how to get the formatting for bulleted text?  Or Centering? Headers? 

Yes, we just add that to the list of available options.   Tomorrow I’ll show you how I pull that into Wordpad so we can build it in PowerShell!

Sean
The EnergizedTech