Field names containing spaces must be wrapped in quotes (single or double).
Example: Update system fields.
Example: Dynamically reference fields by variable.
Using Set-ItemProperty
The PowerShell-standard approach using Set-ItemProperty is also supported.
Example: Update a property using Set-ItemProperty.
Manual Edit Context
For multiple field updates or special scenarios, use explicit BeginEdit and EndEdit calls.
Example: Update multiple fields in a single edit context.
Always match BeginEdit() with EndEdit(). If an error occurs between them, the item remains locked. Consider using try/finally blocks for safety.
Example: Safe edit context with error handling.
Working with Field Types
SPE provides intelligent handling for different Sitecore field types through automated properties.
DateTime Fields
DateTime fields automatically convert between Sitecore's string format and .NET DateTime objects.
Example: Read and write DateTime fields.
Image Fields
Image fields accept item references from the Media Library.
Example: Assign an image to an Image field.
Link Fields (General Link)
Link fields accept item references and automatically create the link XML.
Example: Assign a content item to a GeneralLink field.
Multi-Value Fields
Multi-value fields (Multilist, Treelist, etc.) accept arrays of items.
Example: Assign multiple items to a list field.
Result in Content Editor:
ItemList Assignment
Accessing Typed Fields
Use the ._ or .PSFields property to access strongly-typed field objects.
Example: Access typed Image field properties.
Example: Access typed LinkField properties.
Output:
Example: Find all TextField instances on an item.
When to Use Each Method
Use Automated Properties When:
Updating a single field or a few fields
Readability is important
Working with small datasets
Example:
Use BeginEdit/EndEdit When:
Updating many fields on a single item
Needing explicit control over save timing
Working with system properties (like BranchId)
Example:
Use BulkUpdateContext When:
Updating many items (hundreds or thousands)
Performance is critical
You need to bypass certain Sitecore events
Example:
Automated properties call BeginEdit/EndEdit on every assignment, which can impact performance when updating many fields. Use explicit BeginEdit/EndEdit for multiple updates on the same item.
Bulk Update Patterns
Pattern: Update Items with BulkUpdateContext
Pattern: Update with Progress Reporting
Pattern: Conditional Updates
Pattern: Update from CSV Data
Using Context Switchers
SPE provides the New-UsingBlock function for working with Sitecore context switchers and disposable objects.
Example: Generate relative URL with site context.
Common Context Switchers
Use these classes with New-UsingBlock for various scenarios:
Get-ChildItem -Path "master:\content\home" -Recurse |
Where-Object { $_.TemplateName -eq "Sample Item" -and [string]::IsNullOrEmpty($_.Title) } |
ForEach-Object {
$_.Title = $_.Name # Use item name as title if title is empty
}
# Inefficient - calls BeginEdit/EndEdit for each field
foreach($item in $items) {
$item.Title = "Title" # BeginEdit/EndEdit
$item.Text = "Text" # BeginEdit/EndEdit
$item.Date = [datetime]::Now # BeginEdit/EndEdit
}
# Efficient - single BeginEdit/EndEdit per item
foreach($item in $items) {
$item.Editing.BeginEdit()
$item["Title"] = "Title"
$item["Text"] = "Text"
$item["Date"] = [Sitecore.DateUtil]::ToIsoDate([datetime]::Now)
$item.Editing.EndEdit()
}
# Most efficient for bulk operations
New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext) {
foreach($item in $items) {
$item.Editing.BeginEdit()
$item["Title"] = "Title"
$item["Text"] = "Text"
$item.Editing.EndEdit() > $null
}
}
# BAD - item remains locked if error occurs
$item.Editing.BeginEdit()
$item["Title"] = "New Title" # If this throws, EndEdit never called
$item.Editing.EndEdit()
# GOOD - use try/finally
$item.Editing.BeginEdit()
try {
$item["Title"] = "New Title"
$item.Editing.EndEdit()
} catch {
$item.Editing.CancelEdit()
throw
}
# Be careful with system fields - some should not be modified
$item."__Updated" = [DateTime]::Now # OK - updating audit field
$item."__Updated by" = "sitecore\admin" # OK - updating audit field
# Avoid modifying these unless you know what you're doing:
# $item."__Revision"
# $item."__Created"
# $item."__Created by"
# BAD - throws error if item doesn't exist
$item = Get-Item -Path "master:\content\missing"
$item.Title = "New Title"
# GOOD - check for existence
$item = Get-Item -Path "master:\content\maybe-exists" -ErrorAction SilentlyContinue
if ($item) {
$item.Title = "New Title"
}