Getting Started with Sitecore PowerShell
Sitecore PowerShell is a powerful scripting tool that offers Sitecore developers the flexibility to automate tasks, streamline content management, and perform administrative operations without needing to dive into the depths of the Sitecore API. For those new to Sitecore PowerShell Extensions (SPE), the syntax can seem a bit daunting at first, but with a basic understanding of the most common commands—getters, setters, and removers—you can start scripting efficiently in no time.
In this blog, we'll walk you through a beginner-friendly cheat sheet that outlines key commands to help you hit the ground running. Whether you're looking to retrieve items, update fields, or remove unnecessary content, this guide will provide you with a foundation to automate your Sitecore processes and make your daily development tasks easier. Let's dive into the core commands you'll need to start building powerful Sitecore scripts.
The Getters
"Getters" in this context, are commands that allow you to retrieve or query information from the Sitecore database. They help you access items, fields, or properties without modifying them. These commands are essential for reading and analyzing Sitecore content before performing actions like updates or deletions.
Getting a Sitecore item
Use this to retrieve a single item. Throws an error if the item does not exist.
## using a Sitecore Path
$item = Get-Item -Path "master:\content\path-to-item"
## using an Sitecore GUID
$item = Get-Item -Path "{Item's Sitecore GUID}"
Getting a Sitecore Item’s children
Use to return an item's children (without the -Recurse flag) and grandchildren (using the -Recurse flag).
## using a Sitecore Path
$children = Get-ChildItem -Path "master:\content\path-to-parent-item" -Recurse
## using a Sitecore GUID
$children = Get-ChildItem -Path "{Parent Item's Sitecore GUID}" -Recurse
Getting a Sitecore Item’s Layout
Returns the layout for the specified item. Fields wrapped with [ ] are optional, remove before executing.
## using a Sitecore Item returned from a Get-Item call
$itemLayout = Get-Layout -Item $SitecoreItemObject [-Device <DeviceItem>] [-FinalLayout] [-Language <String[]>]
## using a Sitecore Path
$itemLayout = Get-Layout -Path "master:\content\path-to-item" [-Device <DeviceItem>] [-FinalLayout] [-Language <String[]>]
## using a Sitecore GUID
$itemLayout = Get-Layout -Id "{Item's Sitecore GUID}"[-Device <DeviceItem>] [-FinalLayout] [-Language <String[]>]
Getting a Layout Device
Returns the layout for the specified device.
## Using a device's name
$defaultDevice = Get-LayoutDevice -Name "Default"
## Using Default flag to get the default device
$defaultDevice = Get-LayoutDevice -Default
Getting a Sitecore Item’s Renderings
Returns a RenderingDefinition for an item using the filtering parameters. Fields wrapped with [ ] are optional, remove before executing.
## Using a Sitecore Item
$ItemRenderings = Get-Rendering -Item $SitecoreItem [-DataSource <String>] [-Rendering <Item>] [-Index <Int32>] [-PlaceHolder <String>] [-Parameter <Hashtable>] [-Device <DeviceItem>] [-FinalLayout] [-Language <String[]>]
## Using an Sitecore Path
$ItemRenderings = Get-Rendering -Path "master:\content\path-to-item" [-DataSource <String>] [-Rendering <Item>] [-Index <Int32>] [-PlaceHolder <String>] [-Parameter <Hashtable>] [-Device <DeviceItem>] [-FinalLayout] [-Language <String[]>]
## Using a Sitecore GUID
$ItemRenderings = Get-Rendering -Id "{Sitecore GUID}" [-DataSource <String>] [-Rendering <Item>] [-Index <Int32>] [-PlaceHolder <String>] [-Parameter <Hashtable>] [-Device <DeviceItem>] [-FinalLayout] [-Language <String[]>]
Getting a Sitecore Item’s Rendering Parameters
Gets the available rendering parameters found for a given rendering. Fields wrapped with [ ] are optional, remove before executing.
## using a Sitecore Rendering Definition Object
$RenderingParameters = Get-RenderingParameter -Instance $SitecoreRenderingDefinition [-Name <String[]>]
The Setters
"Setters" are commands used to modify or update items and fields in the Sitecore database. They allow you to change the values of existing fields, update properties, or apply new settings to Sitecore items. Setters are essential for automating content updates and ensuring consistency across your Sitecore environment.
Updating an Item’s fields
Editing an item can be done in three steps. Beginning the editing, making your desired changes and then ending the editing.
## Editing an item's field
$Item.Editing.BeginEdit()
$Item.Fields["Field Name"].Value = "new field value you want to set"
$Item.Editing.EndEdit()
Update an Item’s Layout
Sets an items layout for a device. Fields wrapped with [ ] are optional, remove before executing.
## Using a Sitecore Item
Set-Layout -Item $SitecoreItem -Device $DeviceItem [-Layout <Item>] [-FinalLayout] [-Language <String[]>]
## Using a Sitecore Path
Set-Layout -Path "master:\content\path-to-item" -Device $DeviceItem [-Layout <Item>] [-FinalLayout] [-Language <String[]>]
Update an Item’s Rendering
Updates rendering instance with new values. The instance should be earlier obtained using Get-Rendering. Fields wrapped with [ ] are optional, remove before executing.
## Using a Sitecore Item
Set-Rendering -Item $SitecoreItem -Instance $SitecoreRenderingDefinition [-Parameter <Hashtable>] [-PlaceHolder <String>] [-DataSource <String>] [-Index <Int32>] [-FinalLayout] [-Device <DeviceItem>] [-Language <String[]>]
## Using a Sitecore Path
Set-Rendering -Path "master:\content\path-to-item" -Instance $SitecoreRenderingDefinition [-Parameter <Hashtable>] [-PlaceHolder <String>] [-DataSource <String>] [-Index <Int32>] [-FinalLayout] [-Device <DeviceItem>] [-Language <String[]>]
## Using a Sitecore GUID
Set-Rendering -Id "{Sitecore GUID}" -Instance $SitecoreRenderingDefinition [-Database <String>] [-Parameter <Hashtable>] [-PlaceHolder <String>] [-DataSource <String>] [-Index <Int32>] [-FinalLayout] [-Device <DeviceItem>] [-Language <String[]>]
Update Rendering Parameters
Adds and updates the specified rendering parameter from the rendering. Fields wrapped with [ ] are optional, remove before executing.
Set-RenderingParameter -Instance $SitecoreRenderingDefinition -Parameter $IDictionary [-Overwrite]
The Removers
"Removers" are commands that allow you to delete or remove items from the Sitecore database. These commands are useful for cleaning up unwanted or obsolete content. Removers help you automate content cleanup tasks while ensuring that your Sitecore environment stays organized and clutter-free.
Removing an Item
Used to delete or recycle an item. Accepts items returned by Get-Item or Get-ChildItem.
## Passing the Get-Item function directly into the Remove Item
Get-Item -Path "master:\content\home\delete-me" | Remove-Item
## Passing an already retrieved item into the Remove Item function
$itemVariable | Remove-Item
Removing a Rendering From an Item
Removes renderings from an item based on a number of qualifying criteria. The search criteria are cumulative and narrowing the search in an "AND" manner. Fields wrapped with [ ] are optional, remove before executing.
## Using a Sitecore Item
Remove-Rendering -Item $SitecoreItem -Instance $SitecoreRenderingDefinition [-Device $DeviceItem] [-FinalLayout] [-Language <String[]>]
## Using a Sitecore Path
Remove-Rendering -Path "master:\content\path-to-item" -Instance $SitecoreRenderingDefinition [-Device $DeviceItem] [-FinalLayout] [-Language <String[]>]
## Using a Sitecore GUID
Remove-Rendering -Id "{Sitecore GUID}" -Instance $SitecoreRenderingDefinition [-Device $DeviceItem] [-FinalLayout] [-Language <String[]>]
Removing Rendering Parameters From a Rendering
Removes the specified rendering parameter from the rendering.
Remove-RenderingParameter -Instance $SitecoreRenderingDefinition -Name ["Array", "of", "parameter", "names"]
Your First Steps Toward Sitecore PowerShell Mastery
That’s it! That is everything one would need to know to do some basic level scripting. This blog covered the basics to simply get you started. For a fully detailed breakdown of all methods with all their optional parameters, please visit the official Sitecore PowerShell Extensions documentation here.
You can also check out my other blogs in the Sitecore Development 101 series:
Sitecore Development 101: Creating a Custom Rendering Contents Resolver
Sitecore Development 101: Creating an Interactive PowerShell Script