Skip to main content

Software Development

Basic Powershell Scripting in SXA

Typing

Introduction:

This blog is an introductory post for people who want to start using customized PowerShell scripts in Sitecore SXA. PowerShell scripting is a powerful tool that can greatly reduce the time a task can take. I will go over the basic fundamentals of a PowerShell script, the tips I recommend when writing a script, and give an example script that we will investigate. This blog is for people who have limited coding knowledge, and I will focus on a script that manipulates content items.

The purpose of a good PowerShell script is to increase productivity by automating tasks.  A good PS script does one task efficiently, and as you begin to get more comfortable with writing scripts you can incorporate more complicated functions to achieve more tasks. As we start, you want to make sure you have the correct environment so you have access to scripting (“Setting up your environment” link in the references section). After that, take a look at the “Get to Know Powershell” link to become familiar with the PS environment. Once you have access to scripting, we can take a look at one of the most basic scripts, moving items in the content tree.

The Script:

$rootOfitemsToMove = “master:/sitecore/content/Website/Master Site/Data/Root”
$destinationItem = “master:/sitecore/content/Website/Master Site/Data/Destination”

$moveItems = $True

Write-Host “Moving items from: ” $rootOfitemsToMove” to: ” $destinationItem ” …”

$items = Get-ChildItem -Path $rootOfitemsToMove -Recurse 
foreach ($item in $items){
    Write-Host "Moving " $item.FullPath "to " $destinationItem
    if ($moveItems){
        Move-Item -Path $item.FullPath -Destination $destinationItem
    }
    Write-Host "Item moved"
}

Write-Host "Transfer complete"

We assign PowerShell variables by using the $ character. Variables are powerful tools that can be assigned, manipulated, and accessed. Every PS script will use variables in some way. There is no official naming convention for PS variables, so I use camel case. This script has 4 variables.

Variables:

$rootOfItemsToMove is the root destination of the items we want to move.

$destinationItem is the destination we want those items to move to.

$moveItems is a flag variable. A flag variable is something that is either true or false, and we can use this to change how our script functions. In this case, we are using this variable to actually move the items or not. If $moveItems is false, then the script will only output that an item was moved and where it was moved to, but not actually move them. This flag variable is very helpful for testing, because once you run a PS script, there is no going back! A flag variable in your script can be a good way to check that your script is running as it should be before actually committing to it.

$rootOfitemsToMove = “master:/sitecore/content/Website/Master Site/Data/Root” 
$destinationItem = “master:/sitecore/content/Website/Master Site/Data/Destination”

$moveItems = $True

Write-Host is a simple command that lets you print output to the screen. This is extremely helpful for checking to make sure you are interacting with the correct items, and that your script is running properly. In this script, we have 4 Write-Hosts:

  • The first is just saying where the items are moving from and then inserting the root variable (which will just print out what we set it to in the beginning of the script). Then it is saying where the items are moving to, and inserting the destination variable, and then some ellipses.
  • The second and third Write-Hosts are just printing out the item that is being moved, and if it was successfully moved or not. Since they are inside the for-each loop, they will be printed each time an item is processed. More on the for-each loop later!
  • The fourth is a simple print statement saying that the script has completed.
Write-Host “Moving items from: ” $rootOfitemsToMove” to: ” $destinationItem ” …”

Write-Host "Moving " $item.FullPath "to " $destinationItem

Write-Host "Item moved"

Write-Host "Transfer complete"

$items is the last and most complicated variable we have in this script. It is the variable that will hold our content items that we want to move. Let’s break it down and take a look at it’s parts.

$items = Get-ChildItem -Path $rootOfitemsToMove -Recurse

Here we come across our first Sitecore PowerShell command, Get-ChildItem. Get-ChildItem returns an item’s children and grandchildren. The -Path just designates where it should be getting the children from (our root location). Then we supplement the -Path with our actual root location, which we put in the $rootOfItemsToMove earlier. Then we add a -Recurse. This tells PowerShell to run the Get-ChildItem command on the subitems as well, which ensures we get all the content at our root. Recurse is a powerful tool to make your PowerShell commands more efficient.

ForEach:

Let’s talk about this foreach loop and what’s inside it. A foreach loop is a way to iterate over multiple items. In this loop, we are grabbing one $item from the $items variable (the first item from the items at the root location) and then using the Move-Item command to move it to the destination. When the loop runs again, it will grab the next item. The loop will continue running until all the items in $items have been processed.

foreach ($item in $items){ 
    Write-Host "Moving " $item.FullPath "to " $destinationItem 
    if ($moveItems){ 
        Move-Item -Path $item.FullPath -Destination $destinationItem 
    } 
    Write-Host "Item moved" 
}

We also have an if statement, checking if our flag variable that we defined earlier is true. If it is true then we go ahead and move it! What is in the parentheses will be skipped over if the flag variable is false.

Move-Item takes the path of the item we are moving, which we are accessing by writing $item.FullPath, and then sending it to the destination by using the -Destination parameter and filling it with our destination variable.

Conclusion:

That’s it! A very simple moving PowerShell script. Automating tasks with PowerShell is a great tool to increase efficiency. The “Working with Items” link in the references section is incredibly helpful for finding functions and commands that will help you complete your tasks. The goal of this blog is give you an understanding of how a basic PowerShell script works, so you can use the building blocks to make your own! I hope you found this article helpful!

References:

Setting up your environment: https://doc.sitecorepowershell.com/installation

Get to know powershell: https://doc.sitecorepowershell.com/interfaces/scripting

Working with items: https://doc.sitecorepowershell.com/working-with-items

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.

Kam Ndirangu

Kamiru Ndirangu is a front-end web developer with experience in Sitecore and SXA.

More from this Author

Follow Us
TwitterLinkedinFacebookYoutubeInstagram