1 min read

Sitecore Powershell script to search in Single or Rich text fields

Rich text fields are fun to use. But sometimes it allows your customer to be too creative.
One of our clients added style tags to work around some html layout and css problems. Now the time is right for our developers to actually provide our client with a permanent solution.
In order to find out where these style tags were added, we wrote a script.

This is a PowerShell script that checks every sitecore item in every language and loops through each field.
If the field type is Single-Line Text or Rich Text, we execute a regex checking if there is a style tag in its contents.
I'm creating a custom PowerShell object with the fields that are useful for this script and put it in an ArrayList.
After the foreach loop has finished, I just render the the list by using Format-Table

$startPath = "master:/sitecore/content"
Write-Host "Search started $(Get-Date -format 'u')"

$list = [System.Collections.ArrayList]@()
$itemsToProcess = Get-ChildItem $startPath -Language * -Recurse
if($itemsToProcess -ne $null) {
    $itemsToProcess | ForEach-Object { 
        $match = 0;
        foreach($field in $_.Fields) {
            if($field.Type -eq "Single-Line Text" -or $field.Type -eq "Rich Text") {
                if($field -match 'style=\"[^"]*\"') {
                    $info = [PSCustomObject]@{
						"ID"=$_.ID
						"Language"=$_.Language
						"TemplateName"=$_.TemplateName
						"FieldName"=$field.Name
						"FieldType"=$field.Type
						"FieldValue"=$field
					}
                    [void]$list.Add($info)
                }
            }
        }
    }
}
Write-Host "Search ended $(Get-Date -format 'u')"
Write-Host "Items found: $($list.Count)"
$list | Format-Table

Result from the PowerShell script