RTF Files

Converting RTF to TXT via PowerShell

So I ran into a problem recently where I needed to convert an RTF document back to it’s base text values.  I could do it via a copy & paste, but after digging around a little bit, I happened on an article from Microsoft with some pointers.  How to: Convert RTF to Plain Text (C# Programming Guide)

Now, I have no problems with C# or anything that requires compilation, but I like PowerShell.  So I took what Microsoft gave me and wrote a small function to do the work for me.  Right now it only handles a path to the RTF file for conversion, but can probably be easily extended to allow for other permutations.

Enough talk!  Code, monkey, code!!!

 

5 thoughts on “Converting RTF to TXT via PowerShell

  1. Thank you. This helped me to automate the parsing of rtf files exported from an Outlook OST with libpff. I used this method in a function I just wrote. “https://github.com/oregon-national-guard/powershell/blob/master/ConvertFrom-Rtf.ps1” Cheers

  2. Hey Kevin, I am a complete newbie to powershell and have discovered my need for it with a project that requires doing just this. Would you mind please telling me how to actually make use of your code.?Thank you so much!

  3. Jeremy – here’s a quick code snippet that I’m providing:

  4. Kevin, will this work with .Net 4.7 and PowerShell 5.1? When I try, I get the following error:
    Exception setting “Rtf”: “File format is not valid.”
    At line:5 char:5
    + $Rtb.Rtf = [System.IO.File]::ReadAllText($Path)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

    • Trey – I’m guessing that this is Windows 2012 R2 with .NET 4.7 installed? Am I wrong? I’m trying to duplicate this in my environment so I can test it.

Leave a Comment