| description | Hands-on examples to get you started with practical Sitecore automation. |
|---|
Now that you understand the basics, let's put it all together with practical Sitecore automation tasks. These examples demonstrate real-world scenarios you'll encounter when working with SPE.
Goal: Find all items under /sitecore/content/home that are in a specific workflow state.
# Navigate to the home item
$homeItem = Get-Item -Path "master:\content\home"
# Get all descendants
$items = Get-ChildItem -Path $homeItem.ProviderPath -Recurse
# Filter items in "Draft" workflow state
$draftItems = $items | Where-Object {
$_.Fields["__Workflow state"] -ne $null -and
$_."__Workflow state" -match "{190B1C84-F1BE-47ED-AA41-F42193D9C8FC}"
}
# Display results in an interactive list
$draftItems | Show-ListView -Property Name, ItemPath, "__Updated", "__Updated by"What just happened?
- Retrieved the home item using the Sitecore provider → Learn more
- Queried all child items recursively
- Filtered by workflow field value → Learn more
- Displayed results as a report → Learn more
Goal: Update a field value on multiple items.
# Get items to update
$items = Get-ChildItem -Path "master:\content\home\articles" -Recurse |
Where-Object { $_.TemplateName -eq "Article" }
# Update each item
foreach($item in $items) {
# Begin editing
$item.Editing.BeginEdit()
# Update field
$item["Category"] = "Updated"
# Save changes
$item.Editing.EndEdit()
}
Write-Host "Updated $($items.Count) items" -ForegroundColor GreenKey concepts demonstrated:
- Filtering by template → Templates
- Proper item editing workflow → Editing Items
- Using PowerShell loops and variables
{% hint style="warning" %}
Always use BeginEdit() and EndEdit() when modifying items. Changes won't persist without this pattern.
{% endhint %}
Goal: Create a script that prompts the user for input and processes items based on their choices.
# Prompt user for input
$rootPath = Get-Item -Path "master:\content\home"
$publishItems = $true
$props = @{
Parameters = @(
@{Name="rootPath"; Title="Root Path"; Tooltip="Select the starting location"},
@{Name="templateName"; Title="Template Name";
Source="DataSource=/sitecore/templates&DatabaseName=master&IncludeTemplatesForDisplay=Node,Folder,Template,Template Folder&IncludeTemplatesForSelection=Template";
editor="groupeddroplist"; }
)
Title = "Bulk Item Processor"
Description = "Select items to process"
Width = 500
Height = 300
}
$result = Read-Variable @props
if($result -ne "ok") {
Exit
}
# Process items based on user input
$items = Get-ChildItem -Path $rootPath.ProviderPath -Recurse |
Where-Object { $_.TemplateName -eq $templateName }
Write-Host "Found $($items.Count) items"Interactive concepts:
- Using Read-Variable for user input
- Parameter validation
- Conditional logic based on user choices
- See Interactive Dialogs for more
Ready to experiment? Follow these steps:
In Sitecore, go to Desktop → Development Tools → PowerShell ISE
Try these commands in the script editor:
# Get your home item
Get-Item -Path "master:\content\home"
# List its children
Get-ChildItem -Path "master:\content\home"
# Find items by name
Get-ChildItem -Path "master:\content\home" -Recurse |
Where-Object { $_.Name -like "*Sample*" }Click the Execute button (or press Ctrl+E)
- Examine the object properties returned
- Try modifying the filters
- Experiment with different paths
{% hint style="info" %} The ISE provides IntelliSense, syntax highlighting, and debugging capabilities. It's the best place to learn and develop SPE scripts. See Scripting for more details. {% endhint %}
Test your knowledge with these exercises:
Write a script that finds all items under /sitecore/content that have an empty Title field.
Solution
Get-ChildItem -Path "master:\content" -Recurse |
Where-Object {
[string]::IsNullOrEmpty($_.Fields["Title"].Value)
} |
Show-ListView -Property Name, ItemPath, TemplateNameWrite a script that counts how many items exist for each template under /sitecore/content/home.
Solution
$items = Get-ChildItem -Path "master:\content\home" -Recurse
$templateCounts = $items | Group-Object TemplateName |
Select-Object Name, Count |
Sort-Object Count -Descending
$templateCounts | Show-ListViewExport all items under a specific path to a CSV file with their Name, Path, Template, and Last Updated date.
Solution
$items = Get-ChildItem -Path "master:\content\home" -Recurse
$export = $items | Select-Object Name,
@{Name="Path";Expression={$_.ItemPath}},
@{Name="Template";Expression={$_.TemplateName}},
@{Name="Updated";Expression={$_."__Updated"}}
$export | Export-Csv -Path "$($SitecoreDataFolder)\items-export.csv" -NoTypeInformation
Write-Host "Exported $($items.Count) items"Here are some frequently used patterns you'll encounter:
foreach($item in $items) {
$item.Editing.BeginEdit()
try {
$item["FieldName"] = "Value"
$item.Editing.EndEdit() | Out-Null
}
catch {
$item.Editing.CancelEdit()
Write-Error "Failed to update $($item.ItemPath): $_"
}
}$itemPath = "master:\content\home\test"
if(Test-Path $itemPath) {
$item = Get-Item $itemPath
# Process item
}
else {
Write-Host "Item not found" -ForegroundColor Yellow
}$items = Get-ChildItem -Path "master:\content" -Recurse
$total = $items.Count
$current = 0
foreach($item in $items) {
$current++
Write-Progress -Activity "Processing Items" `
-Status "$current of $total" `
-PercentComplete (($current / $total) * 100)
# Process item here
}Congratulations! You've completed your first SPE scripts. Now:
- Avoid common mistakes: Review Common Pitfalls
- Build on this knowledge: Explore Working with Items
- Create custom reports: Learn Authoring Reports
- Share your scripts: Build a Script Library
{% hint style="success" %} The best way to learn is by doing. Try modifying these examples for your own use cases! {% endhint %}