Code below does what you need.
You set 2 variables first
$rootPath
for the root item where the script should start from
$fieldName
it's the name of your rich text field which you want to update
It goes through the whole tree under rootPath
, checks if the field you want to clean contains any *
character (otherwise no need to proceed for sure) and uses HtmlAgilityPack
library to cleanup content of the field.
Sample input:
<div>
<a href="https://www.skillcore.net" _target="blank">marek musielak</a>
<p>some text</p>
<p style="margin-right: 197.5pt;">
<span>
*<span style="letter-spacing: -0.1pt;"> </span>
*<span style="letter-spacing: -0.05pt;"> </span>*<span style="letter-spacing: -0.1pt;">
</span>*<span style="letter-spacing: -0.05pt;"> </span>*<span style="letter-spacing: -0.1pt;"> </span>* * *
<span style="letter-spacing: -0.1pt;"> </span>*<span style="letter-spacing: -0.05pt;"> </span>*
</span>
</p>
</div>
Sample output:
<div>
<a href="https://www.skillcore.net" _target="blank">marek musielak</a>
<p>some text</p>
</div>
Powershell script:
$rootPath = "master:\content\home"
$fieldName = "Text"
function RemoveAsteriskNodes {
param(
[HtmlAgilityPack.HtmlNode]$node
)
$childNodes = [System.Linq.Enumerable]::ToList($node.ChildNodes);
if ($childNodes.Count -gt 0) {
$childNode = $childNodes[$childNodes.Count - 1]
if ($childNode.InnerText.Contains("*") -and [System.Text.RegularExpressions.Regex]::IsMatch($childNode.InnerText, "^[\s*]+$")) {
$childNode.Remove();
}
else {
RemoveAsteriskNodes $childNode;
}
}
}
$items = @()
$items += Get-Item -Path $rootPath
$items += Get-ChildItem -Path $rootPath -Recurse
foreach ($item in $items) {
$fieldValue = $item[$fieldName]
if ($fieldValue.Contains("*")) {
$doc = New-Object HtmlAgilityPack.HtmlDocument
$doc.LoadHtml($fieldValue);
RemoveAsteriskNodes $doc.DocumentNode
$newFieldValue = $doc.DocumentNode.OuterHtml;
if ($fieldValue -ne $newFieldValue) {
Write-Host $item.Paths.FullPath
$item.Editing.BeginEdit()
$item[$fieldName] = $newFieldValue
$item.Editing.EndEdit() | out-null
}
}
}
*
and remove them (Regex.Replace)