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!!!

function Convert-FromRtf ($Path)
{
    $Rtb = New-Object -TypeName System.Windows.Forms.RichTextBox
    $Rtb.Rtf = [System.IO.File]::ReadAllText($Path)
    $Rtb.Text
    Remove-Variable Rtb -ErrorAction SilentlyContinue
}

7 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

    Reply
  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!

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

    #region First, pre-load the function so that you can use it later
    function Convert-FromRtf ($Path)
    {
        $Rtb = New-Object -TypeName System.Windows.Forms.RichTextBox
        $Rtb.Rtf = [System.IO.File]::ReadAllText($Path)
        $Rtb.Text
        Remove-Variable Rtb -ErrorAction SilentlyContinue
    }
    #endregion
    
    # Declare the path to an RTF File
    $RtfFile = "C:\Users\kevin\AppData\Roaming\Microsoft\Signatures\Super Simple.rtf"
    
    Write-Host "=============This is what it looks like if I just read it=============" -ForegroundColor Red
    Get-Content -Path $RtfFile
    Write-Host "============================Ugly, right?==============================" -ForegroundColor Red
    Write-Host "`n"
    Write-Host "==============This is what it looks like if I convert it==============" -ForegroundColor Green
    Convert-FromRtf -Path $RtfFile
    Write-Host "============================Much better===============================" -ForegroundColor Green
    
    Reply
  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

    Reply
    • 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.

      Reply
  5. Hey Kevin,
    thanks for this article.
    Can I just convert to text and save the text under the same name of the rtf?

    Example: I need to convert about 50 .rtf files to .txt files to use the search function for some keywords in the .txt files ( I cant search for head and foot lines in .rtf (Word))
    So I need to convert from example.rtf ->> example.txt

    Hope you understand my problem and you can help me on this.

    Greetings

    Reply
    • Here’s a snippet that I *think* is doing what you want:
      function Convert-FromRtf ($Path)
      {
      $Rtb = New-Object -TypeName System.Windows.Forms.RichTextBox
      $Rtb.Rtf = [System.IO.File]::ReadAllText($Path)
      $Rtb.Text
      Remove-Variable Rtb -ErrorAction SilentlyContinue
      }

      # Get me a big list of RTF Files – alter for your needs
      $RTFFiles = get-childitem -Path . -Filter “*.rtf” -Recurse
      ForEach ( $RTFFile in $RTFFiles )
      {
      # if I want to search for a specific phrase I can use the -match (regex) or -like (simple string) operators
      if ( ( Convert-FromRtf -Path $RTFFile.FullName ) -match ‘Sparenberg’ )
      {
      Write-Host “I found a match in ‘$( $RTFFile.FullName )’!” -ForegroundColor Green
      }

      # or if I want to go crazy, then I can always just write the contents out to an identical filename (except for the extension)
      # Convert-FromRtf -Path $RTFFile.FullName | Out-File -FilePath $( $RTFFile.FullName.Replace($RTFFile.Extension, “.txt” ) ) -Encoding ascii -Force
      }

      Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.