Skip to main content

Sitecore

Sitecore PowerShell commands – XM Cloud Content Migration

Istock 1536191188

In this post, I’ve listed the most commonly used Sitecore PowerShell commands for content migration. This blog continues from my earlier post: Sitecore XM Cloud Content Migration: Plan and Strategy.

During migration, we created several PowerShell scripts to extract data from the legacy database to CSVs. We then used those CSVs to import content into XM Cloud instances. Based on those scripts, I organized the commands into two groups: Working with Sitecore items and Working with Sitecore renderings. These commands aim to help developers handle similar Sitecore to XM Cloud migrations.

The command snippets are also available as a public GitHub Gist – Most widely used PowerShell SPE commands in Sitecore XM Cloud content migration

Working With Sitecore Items

Create a New Item Using the Template ID

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$item = New-Item -Path $path -Name $itemName -ItemType $itemTemplateId -Language "en"
$item = New-Item -Path $path -Name $itemName -ItemType $itemTemplateId -Language "en"
$item = New-Item -Path $path -Name $itemName -ItemType $itemTemplateId -Language "en"

Sometimes, you may need to create an item with the same ID as in the legacy system to avoid numerous reconfigurations. Especially if those items are being used as the data source. In such a use case, we could use CreateItem from Sitecore.Data.Managers.ItemManager. This method takes the item name, parent item, template ID, and item ID. The passed $id will be the ID of the newly created Sitecore Item. 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$item = [Sitecore.Data.Managers.ItemManager]::CreateItem($name, $parentItem, $templateItem.ID, $id)
$item = [Sitecore.Data.Managers.ItemManager]::CreateItem($name, $parentItem, $templateItem.ID, $id)
$item = [Sitecore.Data.Managers.ItemManager]::CreateItem($name, $parentItem, $templateItem.ID, $id)

Also, there is a ForceId param supported by the ‘New Item‘ function

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
New-Item -Path $path -Name $name -ItemType "Blog Page" -ForceId "3904b0bf-b10b-4fbb-9ced-3de87dfa3d48"
New-Item -Path $path -Name $name -ItemType "Blog Page" -ForceId "3904b0bf-b10b-4fbb-9ced-3de87dfa3d48"
New-Item -Path $path -Name $name -ItemType "Blog Page" -ForceId "3904b0bf-b10b-4fbb-9ced-3de87dfa3d48"

Create a New Item Using the Branch Template

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$item = [Sitecore.Data.Managers.ItemManager]::AddFromTemplate($itemName, $branchTemplateId, $parentItem)
$item = [Sitecore.Data.Managers.ItemManager]::AddFromTemplate($itemName, $branchTemplateId, $parentItem)
$item = [Sitecore.Data.Managers.ItemManager]::AddFromTemplate($itemName, $branchTemplateId, $parentItem)

Checking if the Path Exists in the Content Tree

In use cases, we need to check whether the path exists before creating an item on that path. 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$pathExists = Test-Path -Path $path
if($pathExists)
{
//logic
}
$pathExists = Test-Path -Path $path if($pathExists) { //logic }
$pathExists = Test-Path -Path $path
if($pathExists)
{
  //logic
}

Copying Items

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Copy-Item -Path $sourcePath -Destination $targetPath
Copy-Item -Path $sourcePath -Destination $targetPath
Copy-Item -Path $sourcePath -Destination $targetPath

Working with Sitecore Renderings

Get All Renderings for an Item

This script was used to analyze an item’s legacy renderings, map them with new XM cloud renderings (components), and map fields. 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$item = Get-Item -Path $path -Version "latest"
$resultObj = @()
$defaultLayout = Get-LayoutDevice"Default"
Get-Rendering -Item $item -Device $defaultLayout -FinalLayout | ForEach{
$renderingItem = Get-Item -Path master: -ID $_.ItemID
$Obj = @{
RenderingName = $renderingItem.Name
RenderingId = $_.ItemID
DataSource = $_.Datasource
Placeholder = $_.Placeholder
PageItem = $_.OwnerItemID
}
$resultObj += New-Object psobject -Property $Obj
}
$resultObj | Format-Table RenderingName, RenderingId, DataSource, Placeholder, PageItem
$item = Get-Item -Path $path -Version "latest" $resultObj = @() $defaultLayout = Get-LayoutDevice "Default" Get-Rendering -Item $item -Device $defaultLayout -FinalLayout | ForEach { $renderingItem = Get-Item -Path master: -ID $_.ItemID $Obj = @{ RenderingName = $renderingItem.Name RenderingId = $_.ItemID DataSource = $_.Datasource Placeholder = $_.Placeholder PageItem = $_.OwnerItemID } $resultObj += New-Object psobject -Property $Obj } $resultObj | Format-Table RenderingName, RenderingId, DataSource, Placeholder, PageItem
$item = Get-Item -Path $path -Version "latest"
$resultObj = @()
$defaultLayout = Get-LayoutDevice "Default"
Get-Rendering -Item $item -Device $defaultLayout -FinalLayout | ForEach {
    $renderingItem = Get-Item -Path master: -ID $_.ItemID
    $Obj = @{
        RenderingName =  $renderingItem.Name
        RenderingId =  $_.ItemID
        DataSource = $_.Datasource
        Placeholder = $_.Placeholder
        PageItem = $_.OwnerItemID
}
    $resultObj += New-Object psobject -Property $Obj
}
$resultObj | Format-Table RenderingName, RenderingId, DataSource, Placeholder, PageItem

Create and Set Rendering for an Item

When importing data from CSV, we often need to create and set a data source to render an item.  For this use case, I created a function that takes the rendering ID, the placeholder to add the rendering, and the data source ID. 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function CreateAndSetRendering{
param([String]$id,[String]$placeholder,[String]$dsid
)
$renderingId = [Sitecore.Data.ID]::Parse($id)
$rendering = get-item -path master: -id $renderingId
$renderinginstance = $rendering | new-rendering -placeholder $placeholder
if($dsid -ne "")
{
$datasourceId = [Sitecore.Data.ID]::Parse($dsid)
$renderinginstance.datasource = $datasourceId
}
add-rendering -item $item -placeholder $placeholder -instance $renderinginstance -finallayout
$item.editing.beginedit()
$item.editing.endedit() | out-null
}
function CreateAndSetRendering{ param([String]$id,[String]$placeholder,[String]$dsid ) $renderingId = [Sitecore.Data.ID]::Parse($id) $rendering = get-item -path master: -id $renderingId $renderinginstance = $rendering | new-rendering -placeholder $placeholder if($dsid -ne "") { $datasourceId = [Sitecore.Data.ID]::Parse($dsid) $renderinginstance.datasource = $datasourceId } add-rendering -item $item -placeholder $placeholder -instance $renderinginstance -finallayout $item.editing.beginedit() $item.editing.endedit() | out-null }
function CreateAndSetRendering{
    param([String]$id,[String]$placeholder,[String]$dsid
        )
        
        $renderingId = [Sitecore.Data.ID]::Parse($id)
        $rendering = get-item -path master: -id $renderingId
        $renderinginstance = $rendering | new-rendering -placeholder $placeholder
        if($dsid -ne "")
        {
            $datasourceId = [Sitecore.Data.ID]::Parse($dsid)
            $renderinginstance.datasource = $datasourceId
        }
        add-rendering -item $item -placeholder $placeholder -instance $renderinginstance -finallayout
        $item.editing.beginedit()
        $item.editing.endedit() | out-null
}

Retrieve the Rendering and Remove From the Presentation

{3904b0bf-b10b-4fbb-9ced-3de87dfa3d48} is the Sitecore Item ID of the rendering item we wish to retrieve

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$defaultLayout = Get-LayoutDevice"Default"
$rendering = Get-Rendering -Item $item -Device $defaultLayout -FinalLayout | Where-Object{$_.ItemID -eq "{3904b0bf-b10b-4fbb-9ced-3de87dfa3d48}"}
Remove-Rendering -Item $item -Instance $rendering -Device $defaultLayout -FinalLayout
$defaultLayout = Get-LayoutDevice "Default" $rendering = Get-Rendering -Item $item -Device $defaultLayout -FinalLayout | Where-Object { $_.ItemID -eq "{3904b0bf-b10b-4fbb-9ced-3de87dfa3d48}"} Remove-Rendering -Item $item -Instance $rendering -Device $defaultLayout -FinalLayout
$defaultLayout = Get-LayoutDevice "Default"
$rendering = Get-Rendering -Item $item -Device $defaultLayout -FinalLayout | Where-Object { $_.ItemID -eq "{3904b0bf-b10b-4fbb-9ced-3de87dfa3d48}"}
Remove-Rendering -Item $item -Instance $rendering -Device $defaultLayout -FinalLayout

Getting a Specific Rendering Parameter Value

$paraName is the rendering parameter name, for example, “Styles”.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$rendering = Get-Item -Path master: -Id "{3904b0bf-b10b-4fbb-9ced-3de87dfa3d48}"
$renderingItem = Get-Rendering -Item $item -Device $defaultLayout -Rendering $rendering -FinalLayout
$parameterValue = Get-RenderingParameter -Rendering $renderingItem -Name $paramName
$rendering = Get-Item -Path master: -Id "{3904b0bf-b10b-4fbb-9ced-3de87dfa3d48}" $renderingItem = Get-Rendering -Item $item -Device $defaultLayout -Rendering $rendering -FinalLayout $parameterValue = Get-RenderingParameter -Rendering $renderingItem -Name $paramName
$rendering = Get-Item -Path master: -Id "{3904b0bf-b10b-4fbb-9ced-3de87dfa3d48}"
$renderingItem = Get-Rendering -Item $item -Device $defaultLayout -Rendering $rendering -FinalLayout
$parameterValue = Get-RenderingParameter -Rendering $renderingItem -Name $paramName

Updating Rendering Parameter Value

If there are more than one rendering of the same type, the returned $renderingItem will be an array so you can access the first rendering parameters $renderingItem[0].Parameters: This will return all parameters, and then you will have to check for a specific parameter.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$rendering = Get-Item -Path master: -Id "{3904b0bf-b10b-4fbb-9ced-3de87dfa3d48}"
$renderingItem = Get-Rendering -Item $item -Device $defaultLayout -Rendering $rendering -FinalLayout
$renedringParams = $renderingItem[0].Parameters
$styles = "Styles"
if($renedringParams.Contains($styles)){
$renedringParams = @{
Styles = "%7B3904b0bf-b10b-4fbb-9ced-3de87dfa3d48%7D"
}
}
Set-RenderingParameter -Instance $renderingItem[0] -Parameter $renedringParams | Out-Null
Set-Rendering -Item $item -Instance $renderingItem[0] -FinalLayout
$rendering = Get-Item -Path master: -Id "{3904b0bf-b10b-4fbb-9ced-3de87dfa3d48}" $renderingItem = Get-Rendering -Item $item -Device $defaultLayout -Rendering $rendering -FinalLayout $renedringParams = $renderingItem[0].Parameters $styles = "Styles" if ($renedringParams.Contains($styles)) { $renedringParams = @{ Styles = "%7B3904b0bf-b10b-4fbb-9ced-3de87dfa3d48%7D" } } Set-RenderingParameter -Instance $renderingItem[0] -Parameter $renedringParams | Out-Null Set-Rendering -Item $item -Instance $renderingItem[0] -FinalLayout
$rendering = Get-Item -Path master: -Id "{3904b0bf-b10b-4fbb-9ced-3de87dfa3d48}"
$renderingItem = Get-Rendering -Item $item -Device $defaultLayout -Rendering $rendering -FinalLayout
$renedringParams = $renderingItem[0].Parameters
$styles = "Styles"
 if ($renedringParams.Contains($styles)) {
      $renedringParams = @{
            Styles = "%7B3904b0bf-b10b-4fbb-9ced-3de87dfa3d48%7D"
    }
    }
Set-RenderingParameter -Instance $renderingItem[0] -Parameter $renedringParams | Out-Null
Set-Rendering -Item $item -Instance $renderingItem[0] -FinalLayout

Note: We must embed Sitecore ID for your required style between %7B and %7D. For multiple values, the separator is %7D%7C%7B. It’s how Sitecore stores params values.

You can store multiple values like this: Styles = “%7B3904b0bf-b10b-4fbb-9ced-3de87dfa3d48%7D%7C%7B936219ee-a03b-49c5-8eff-8b877b5c1319%7D”

Conclusion

So, this is the consolidated list of Sitecore PowerShell commands for content migration. The IDs used in the above snippets were not valid Sitecore item IDs. Replace them with valid Sitecore item IDs based on the Sitecore items used in your project.

Keep learning!

Thoughts on “Sitecore PowerShell commands – XM Cloud Content Migration”

  1. This is a highly practical and well-structured guide for Sitecore XM Cloud migrations. The breakdown between working with items and renderings is very helpful, especially the inclusion of PowerShell examples for creating items with specific IDs and managing renderings programmatically. One suggestion would be to include some error-handling patterns or common pitfalls to watch for—like what happens when a rendering’s data source is missing or invalid. Also, a downloadable script set or GitHub repo link could be useful for teams executing large-scale migrations. Thanks for sharing this valuable resource!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Akash Borkar

Akash Borkar is a passionate IT professional with a strong focus on continuous learning and knowledge sharing. Currently serving as a Senior Technical Consultant at Perficient, he brings hands-on experience in technologies like .NET Core, Sitecore, and Optimizely CMS.

More from this Author

Categories
Follow Us