Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell change RTF Document and save as RTF Document

I want to change the content of a RTF document and than save it as RTF document:

$defaultRtfFile>> "C:\Users\user\Desktop\Outlokk-Signature\Test.rtf"

When I do it like this ,after I changed the content, I can't open it in word (I can but there are some strange characters).

When I try it like this:

$Rtb = New-Object -TypeName System.Windows.Forms.RichTextBox
$Rtb.Rtf = [System.IO.File]::ReadAllText("C:\Users\fwohlgemuth\Desktop\Outlokk-Signature\DefaultFiles\default.rtf")
$Rtb.Text.Replace($bName,$ADDisplayName)

After saving it nothing is changed but in power shell it is changed and the hyper-links behind images are now not hidden behind the image.

When I make 2 Replace one of them isn't more visible.

After changing the rtf I have to change a htm document I think I will get there the same problem.

Thy for help :)

like image 971
Felix ccad Avatar asked Feb 19 '26 23:02

Felix ccad


1 Answers

Use the Get-Content cmdlet to load the file, do your replacements and finally write it back using the Set-Content cmdlet.

Example:

$filepath = 'Your_file_Path'
$content = Get-Content $filepath -raw
$content = $content -replace 'ReplaceMe', 'IReplacedYou'
$content = $content -replace 'ReplaceMe2', 'IReplacedYou2'
$content | Set-Content $filepath
like image 88
Martin Brandl Avatar answered Feb 22 '26 14:02

Martin Brandl