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
}
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
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!
Jeremy – here’s a quick code snippet that I’m providing:
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.
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
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
}