PowerShell Script to update Alt tag missing for Images

Hello everyone. I am back with another powershell script that can be very useful if you want to update alt tag missing for all images in sitecore.We all know, how important is alt tag from SEO perspective and that's why, this powershell script can come very handy when we have lot of missing alt tag for images.

Below is the powershell script for your reference.The script is pretty simple to understand.

       
$rootPathOfImages = "/sitecore/media library/Project/"
$testMode = $false
$secondsToWaitOnLimitError = 60
$successCount = 0
$errorCount = 0
$errorList = ""
$date = Get-Date
$formattedDate =$date.tostring('dd-mmm-yyyy-hhmmss')

# List of template ids to exclude from processing
$excludedTemplateIds = @(
    "{FE5DD826-48C6-436D-B87A-7C4210C7413B}", #media folder
    "{0603F166-35B8-469F-8123-E8D87BEDC171}", #pdf
    "{16692733-9A61-45E6-B0D4-4C0C06F8DD3C}", #doc
    "{7BB0411F-50CD-4C21-AD8F-1FCDE7C3AFFE}", #docx
    "{962B53C4-F93B-4DF9-9821-415C867B8903}", #unversioned file
    "{E76ADBDF-87D1-4FCB-BA71-274F7DBF5670}", #unversioned movie
    "{4F4A3A3B-239F-4988-98E1-DA3779749CBC}" #unversioned zip
)

# List of folder ids to exclude from processing
$excludedFolderIds = @(
    "{23FAF40D-6700-480C-BCAD-92E23F60634C}" #wffm folder
)

if($testMode){
    Write-Host -Foreground Yellow "Running in TEST MODE, no Alt Text will be set."
}
                    
Write-Host "Finding Images with Missing Alt Text in root folder: $($rootPathOfImages) ..."

cd $rootPathOfImages
$images = Get-ChildItem -Recurse . | Where-Object {
                                        ($excludedTemplateIds -notcontains $_.TemplateId) `
                                        -and ($excludedFolderIds -notcontains $_.ID) `
                                        -and ($excludedFolderIds -notcontains $_.Parent.ID) `
                                        -and ($excludedFolderIds -notcontains $_.Parent.Parent.ID) `
                                        -and ($excludedFolderIds -notcontains $_.Parent.Parent.Parent.ID) `
                                        -and ([string]::IsNullOrWhiteSpace($_.Fields["Alt"].Value)) `
                                    }

Write-Host -Foreground Green "$($images.Count) Images Found With Missing Alt Text"

$result = @()
$counter = 0

foreach ($image in $images)
{
    $counter+=1
    
    # Sitecore
    
    try{
        Write-Host "Processing Image $($counter) of $($images.Count): $($image.ID) - $($image.Name) ...`n"
        $id = $image.ID
        $name = $image.Name
        $displayName = $image.Fields["__Display name"].Value
        $path = $image.ItemPath
        $success = $False
		
		$altText = $name.Replace("-"," ").Replace("_"," ")
		
		$image.Editing.BeginEdit()
        $image.Fields["Alt"].Value = $altText
        $image.Editing.EndEdit()
    
		$successCount +=1
    }
    catch
    {
        Write-Host -Foreground Red "Error Processing Image: $($id) - $($name) - Error Message: $($_.Exception.Message)"
        continue
    }

    
}

Write-Host -Foreground Green "$($successCount) Images Successfully Processed"
Write-Host "$($errorCount) Errors Processing Images"

if($errorCount -gt 0){
    Write-Host -Foreground Red "Errors List"
    Write-Host -Foreground Red "-----------------------------"
    Write-Host -Foreground Red $errorList
}



	   
 

We are using the image name to update the alt tag value.

Hope this will help. Thanks for reading. Happy learning!!

You can check my other blogs blog related to Sitecore

Comments

Popular posts from this blog

Sitecore XM Cloud Form Integration with Azure Function as Webhook

Automate RSS Feed to Sitecore XM Cloud: Logic App, Next.js API & Authoring API Integration

Create and Fetch Content From Sitecore Content Hub One using GraphQL and React