Need means to read RTF file text content w PoSh cmd line scripts

My need to to incorporate numerous RTF text to regenerate individual HTML files using command line automation. Loading the PoSh GUI is not going to work for me, so I need to use the CMD Powershell -file approach. The Object types seem to be undefined when running Powershell command line scripts as compared to the Powershell GUI.

I found a number of solutions for loading RTF file text content using Windows or Word objects; however, these do not work from the command line and either throw invalid object type errors or do not separate the text from the RTF file metadata like “\par” Thanks.

AndyB1580,
Welcome to the forum. :wave:t4:

You choose the category “Open Discussions” for your post. Hat exactly is it what you want to discuss?

And what is

?

Are you refering the PowerShell_ISE?

If you run scripts triggered by the Windows task scheduler using MSFT Office apps you should be aware of the fact that they are are not meant to be used in a non interactive session and are notorious for not working reliably in such a use case.

You may change the category of your post if you have a specific question about some self written code and you may post this question then along with the relevant part of your code. :wink:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

You are correct. I need not to use the Powershell IDE.

Invoked this way from Windows CMD shell script which recurses through folder structure:
powershell .\test.ps1

Here are the means of reading RTF files that I tested.

Solution 1:

Load *.rtf file into a hidden .NET RichTextBox

$rtBox = New-Object System.Windows.Forms.RichTextBox
$rtfText = [System.IO.File]::ReadAllText($src_file);
$rtBox.Rtf = $rtfText

Result Error Message:
New-Object : Cannot find type [System.Windows.Forms.RichTextBox]: …

Solution 2:

$data = New-Object System.Windows.Forms.DataObject
$rtf = Get-Content -Path $RtfFile -raw
($rtf.ToString())

Result RTF metadata mixed with Text content:
{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}{\f3\fcharset0 Arial;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs28\f3\cf0 \cf0\ql{\f3 {\ltrch …

Without the -RAW parameter, the returned content is a null set.

Thanks!

I still do not know at all what your question is and how we can help you.

If you have a question or an issue with code you’ve got from GitHub you may contact the author.

Please keep in mind: we do not review, refactor or debug code you’ve found on the internet on request. And of course we cannot see your screen and we cannot read your mind. :wink:

It would be helpful if you post something what’s on StackOverflow called “Minimal, Reproducible Example”

People comming here to help others do this voluntarily. So it’s a kind of showing respect to them to make it as easy as possible for them to actually help you.

Having your initial question in mind …

Sometimes better than dealing with some circumstances is to avoid them. :wink: I’d consider RTF text not to be an easy to handle and reliable data source format to be used for automation. If you have the Chance to get the source data in an easier computer readable format you would make your life a lot easier and probably your solution a lot more reliable and robust.

1 Like

I found the solution. For the benefit of the next person, the online examples that I cited were simply missing the following line to enable them to run without the IDE. It was that simple.

add-type -AssemblyName System.Windows.Forms