Add PowerShell Script to the Context Menu in Sitecore SXA

Person coding on multiple screens

This blog describes how to make any PowerShell script appear in the context menu of an item in Sitecore SXA. As a result, these menu options should only be available when necessary. We’ll also see ShowRule and EnableRule fields in the PowerShell script items for hiding/showing and enabling/disabling based on some fundamental Sitecore rules.

Context Menu – When we right-click on any Sitecore item, a menu with multiple options such as Insert, search, duplicate, and so on appears. This menu is known as the Item Context Menu.

PowerShell Script in Context Menu – In Sitecore, we can add PowerShell scripts to the scripts area of the item context menu. The goal of adding Powershell scripts to the context menu is to make it possible to run the script from any item. As a result, it becomes more author-friendly.

Interactive PowerShell Scripts – We can create an interactive PowerShell script using the Sitecore PowerShell Extension. This improves an author’s UI experience by displaying dialog boxes with results, outputs, or dialog boxes for taking any inputs from them.

Powershell Script in Context Menu

Let’s Add PowerShell Script to the Context Menu in Sitecore SXA

Let’s start with a simple business scenario: suppose we have a “Zip Codes” list in a CSV file.

  1. We should be able to run the file import on a specific data folder only.
  2. We want all Zip Codes items automatically created inside the target folder. (Assumption: we have the template for the zip code item)

So, in this section, we will write an interactive PowerShell script. This will prompt the user to upload a CSV file via interactive dialogue. We will add the hand to the context menu. This script option should be available only for Zip Code Folder and hidden for any other Sitecore item.

Step 1: Create A PowerShell Script Module.

When we create a Powershell script, we need to save it somewhere. Sitecore PowerShell keeps all PowerShell scripts in the Script Library (PowerShell Script Library).

The script library is located at “Sitecore>System>Modules>PowerShell>Script Library.” It organizes the script under modules for easier access.

  1. Under the ‘SPE’ folder, we will create a folder with a module folder template for our project.
  2. Creating a new folder with my project name, “DailySitecore.”

Now, right-click the project folder and select Insert. There are several insert options available here. As shown in the image below, we will select “Module Wizard” to create a new module within our project folder.

Add PowerShell Script Module using Module Wizard

To begin the process of creating a new PowerShell script module. When you click on the Module Wizard, a dialogue box will appear, asking for input. Because we only need this module for the context menu, I have only provided the inputs I require.

  1. Give the module a meaningful name.
  2. Check the “Enable Module” checkbox
  3. Choose “Content Editor – Context Menu” from the list
  4. Proceed

Create New Module - PowerShell Module Wizard

A module folder will be created when you click the “Proceed” button. The module folder will have the “Content Tree” and “Context Menu” folders.

Sitecore - Understanding Development Approaches: A Sitecore Outlook
Understanding Development Approaches: A Sitecore Outlook

Designing, building, and implementing top-notch experiences not only requires a great deal of planning, strategy, and time – it also requires the right digital experience platform (DXP) and the right development approach for your business needs.

Get the Guide

Now that your PowerShell script module has been successfully created. You can keep the PowerShell script in the Context Menu folder.

Note: All scripts placed in this folder will now be visible in the Context Menu’s Scripts sections for all Sitecore items.

Step 2: Create Interactive PowerShell Script for CSV Import.

Following the creation of the module, we will write an interactive PowerShell script using PowerShell ISE. Here, we can refer to the documentation to learn how to create interactive dialogs using Sitecore PowerShell.

Go to the Launchpad and launch the PowerShell ISE, then write the script shown below.

# Upload CSV file
#It will create the folder if it is not found
$dataFolder = [Sitecore.Configuration.Settings]::DataFolder
$tempFolder = $dataFolder + "/tempupload"
$filePath = Receive-File -Path $tempFolder -overwrite -Description "Please Upload a CSV file" -Title "Data Import"
if($filePath -eq "cancel"){exit} 

$importRows = Import-CSV $filePath
$index = 1;
$totalRows = $importRows.Count;
$PercentComplete = 0;
New-UsingBlock (New-Object Sitecore.Data.BulkUpdateContext){ 
    #Data Folder Location
    $item = Get-Item .
    $zipCodeFolderPath = $item.Paths.FullPath

    #Template Location
    $zipCodeTemplatePath = "/sitecore/templates/Project/DailySitecore/Zip Code"

    #Field Name
    $zipcodeItemField = "ZipCode"

    #Check If All Path are Valid
    Function Show-PathNotFound-Message($Path){
        Show-Alert -Title "'$Path' doesn't exist!"
        Write-Host "'$Path' doesn't exist!" -ForegroundColor Red; 
    if (-Not (Test-Path $zipCodeFolderPath)) {

    #Get all entries from CSV
    foreach ($row in $importRows)
        Write-Progress -Activity "Processing records ($index / $totalRows)" -Status "$PercentComplete% Complete:" -PercentComplete $PercentComplete

        $zipCode = $row.Zipcode
        Function Show-ErrorMessage($msg){
            Show-Alert -Title "$msg"
            Write-Host $msg -ForegroundColor Red; 
        if($zipCode -eq $null){
            Show-ErrorMessage("CSV should contains header row as Zipcode");
        #Adding ZipCode if Not present
        $zipCodeItemPath = $zipCodeFolderPath+"/"+$zipCode
        if(Test-Path -Path $zipCodeItemPath){
            Write-Host "$zipCode is already Exits" -ForegroundColor Yellow;
            $item  = New-Item -Path $zipCodeFolderPath -Name $zipCode -ItemType $zipCodeTemplatePath
            $item[$zipcodeItemField] = $zipCode
            $newZipId = $item.ID;
            Write-Host "$index Item created for: $zipCode";
     $PercentComplete = [int] (($index / $totalRows) * 100)

After you’ve finished, click the save button and navigate to your module’s Context Menu folder. Enter a name for the script and press the Select button.

Save Script to PowerShell Script Module

Congratulations! Your script is now complete and appears in the Context Menu. Right-click on any item to confirm that you can now see the script in the menu, as shown in the image below.

PowerShell Script added to Context Menu for All Items

But wait, the script will be displayed when you right-click on any item in the Sitecore tree. However, in this case, we only need this script to appear in the Zip Code Data Folder (An item created from a specific template). To accomplish this, we will need to add some rules to the script, which we will see in the next step.

Step 3: Setting up Rules for Script (Show-Rule & Enable-Rule)

Every Powershell script item has two fields: ShowRule and EnableRule. The show-rule and enable-rule are used to control the visibility of the script in the context menu for any item.

ShowRule: This field displays the script option only when the scenario matches the given condition.

EnableRule: This field enables or disables the script option only when the scenario matches the given condition. The script will still be visible but disabled if the condition does not match.

Follow the steps outlined below.

  1. Choose the script item.
  2. Navigate to the Interactive Section and then to the “ShowRule” / “EnableRule” Field
  3. Edit Rules > Add the necessary rule (as shown below)

Show Rule and Enable Rule Fields in PowerShell Script Item

Only items created from the template “Zip Codes Folder” will display this script. The script option will not be available in the context menu for other Sitecore items.

Note: If you cannot see the fields, this may be due to security concerns; in this case, click on ‘Elevate Session’ at the top. It will prompt you for credentials before displaying all fields.

Elevate Session if fields not visible

Step 4: Change the Appearance of the Script

We can also change how the script option appears. To give the authors a more descriptive appearance. Changing the appearance is similar to changing the appearance of other Sitecore items.

We will change the script’s icon and want it to appear in the top second by order in the context menu.

  1. In our case, select the script item ‘Zip Code Import’ from the CSV Import module in Script Library.
  2. Go to the View tab and enable standard fields to view.
  3. Modify the Icon in the Icon field (select the relevant icon concerning the script’s working).
  4. Modify the sort order (to specify an order of appearance in the menu).

Change appearance of Sitecore Item

Our script is now ready to appear in the context menu of the Zip Code Folder item.

I hope you found this helpful article. Happy Learning!!

Thoughts on “Add PowerShell Script to the Context Menu in Sitecore SXA”

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.

Ravindra Mishra

Ravindra Mishra is Technical Consultant at Perficient Nagpur GDC, experienced in ASP.NET MVC, Sitecore, SXA, and JavaScript. He likes to learn and explore various technologies and understand the architecture beneath them.

More from this Author

Follow Us