Sitecore PowerShell Script to Update a Field


Today I am going to share a PowerShell script that can be used to change the value of the field. Moreover, it works for all types of fields based on the name of the field in Sitecore.

In my recent project, I got a requirement where we had to change all the Single Line and Rich Text Fields having the name “Product Slot” to a new value. The value must be the same for all the fields. Since the count of those items is large, I prepared a PowerShell script to fix them all in one go.

# Get all the descendant items from the Sitecore content tree
$items = Get-ChildItem -Path "master:/sitecore/content" -Recurse

# Replace with required template ID
$templateId = "{AB86861A-6030-46C5-B394-E8F99E8B87DB}"

# Filtering the items having required template ID
$itemsWithRequiredTemplate = $items | Where-Object { $_.TemplateId -eq $templateId }

# Updating the items
$itemsWithRequiredTemplate | ForEach-Object {
	$_.Editing.BeginEdit()
	$_.Fields["Product Slot"].Value = "MAX2085PMX32";
	$_.Editing.EndEdit()
}

PowerShell scripts are useful as they are development accelerators that increase productivity and decrease the time required to deliver a solution.


The PowerShell script is simple. It first gets all the items in a recursive manner from the path you specified. Then it filters items based on the template ID you specified. At last, the value for the field is changed.

References


Chirag Goel

I am a developer, likes to work on different future technologies.

Post a Comment (0)
Previous Post Next Post