PowerShell to update Alt tag for Images in Media Library

The alt tag for the images plays a very important role in the SEO of the web pages which has image tag. So we thought of adding the token in the standard values of the image template (/sitecore/templates/System/Media/Unversioned/Image/__Standard Values).

This will handle the Alt tags for all the new images that will be added to the media library but it will not change anything for the existing images in the media library so we will need to update it using Sitecore PowerShell script below:

$imagesToBeUpdated = "master:/sitecore/media library"
$images = Get-ChildItem -Path $imagesToBeUpdated -Language * -Recurse | 
Where-Object { ($_.Fields["Alt"] -ne $null) -and ($_.Fields["Alt"].Value -eq '' -or $_.Fields["Alt"].Value -eq '$name') }

$images |
ForEach-Object {
write-host "Updating Alt text for - " $_.Name
$_.Editing.BeginEdit() 
$_["Alt"] = $_.Name
$_.Editing.EndEdit() | Out-Null } 

The above script makes sure that if there is an Alt value defined for any images then it is not overridden.

Reference Link: https://www.sitecoreing.com/2020/06/updating-sitecore-image-alt-text.html

Thank you.. Keep Learning.. Keep Sitecoring.. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s