Avatar of E=mc2
E=mc2
Flag for Canada asked on

Modify Powershell Script to remove asterisk from HTML file

I would like to modify this Powershell script to remove an asterisk which is appearing in the html file.

The current error I am getting is:

The regular expression pattern * is not valid.
At C:\Users\User1\Desktop\Report.ps1:39 char:1
+ ((Get-Content -path $writeto -Raw) -replace $oldcontent,$newcontent)  ...


$oldcontent = '*'
$newcontent = '<span style="color: rgb(51, 102, 255); font-weight: bold;"'
$writeto = 'C:\Users\User1\Desktop\Report.html'

((Get-Content -path $writeto -Raw) -replace $oldcontent,$newcontent) | Set-Content -Path $writeto


Open in new window

PowershellHTMLScripting Languages

Avatar of undefined
Last Comment
Sam Jacobs

8/22/2022 - Mon
Sam Jacobs

An asterisk is a special character in a regular expression.
You need to escape it:
$oldcontent = '`*' 
$newcontent = '<span style="color: rgb(51, 102, 255); font-weight: bold;"' 
$writeto = 'C:\Users\User1\Desktop\Report.html' ((Get-Content -path $writeto -Raw) -replace $oldcontent,$newcontent) | Set-Content -Path $writeto

Open in new window

It's a backwards apostrophe/grave (the character under the tilde). 
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Sam Jacobs

Yikes! I can't believe I mixed up my PowerShell and RegEx escape characters.
I guess I need to stop responding late at night.
Thanks, @oBdA. 
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck