How to convert SPE scripts to the file system.

As you probably know, Sitecore Powershell Extension is full of useful scripts that can be used as learning resources and sources of inspiration.

I use Visual Studio Code to edit Powershell scripts much more conveniently. So I started working on the concept that I write scripts in VS Code, and then they are to be automatically found in Sitecore as SPE scripts. The first step is, therefore, the conversion of SPE items that contain scripts to files on the disk. Such files can be easily edited in the VS Code.

An additional advantage is a fact that I can save all scripts from SPE and SXA to disk, where it is much more convenient to search them.

As a result of my work, a script was created to convert SPE items to Powershell files.

function Convert-ScriptItemToFile {
Convers a SPE item to a file on disk.
Autor: Robert Senktas (@RobsonAutomator)
[CmdletBinding(SupportsShouldProcess = $True)]
param (
[parameter(ValueFromPipelineByPropertyName, ValueFromPipeline)]
# The root path for SPE module scripts. This will be replaced with $FileRoot parameter.
$ScriptRoot = "/sitecore/system/Modules/PowerShell/Script Library",
# Folder on disk where items will be saved as files
process {
# build a filesystem path
$scriptPath = $Item.Paths.Path -replace $ScriptRoot,$FileRoot -replace "/","\"
$scriptFolder = Split-Path $scriptPath Parent
$whatifMessage = "Save script to a file $scriptPath.ps1"
If ($PSCmdlet.ShouldProcess($Item.ID, $whatifMessage)) {
# create a folder structure
mkdir $scriptFolder Force | Out-Null
# save script to file
$encoding = New-Object System.Text.UTF8Encoding
[System.IO.File]::WriteAllText("$scriptPath.ps1", $Item."Script", $encoding)

OK ,function Convert-FileToScriptItem is ready. Now you have to provide SPE items that will be converted for files. I used the function Find-Item which searches for items using the Sitecore Content Search API.

# Find all Powershell scripts in Sitecore
$criteria = @(
    @{Filter = "Equals"; Field = "_templatename"; 
Value = "PowerShell Script"}

$props = @{
    Index = "sitecore_master_index"
    Criteria = $criteria

$speScripts = Find-Item @props

Everything is ready to convert. We have SPE items; we have a convert function, let’s run in together.

$diskRoot = Join-Path -Path $apppath -ChildPath "App_Data\SPE"

# Save all SPE scripts to  the filesystem
foreach( $script in $speScripts)
    Get-Item -Path "master:$($script.Path)" | `
    Convert-ScriptItemToFile -FileRoot $diskRoot -Verbose -WhatIf

For your convenience, I added at the end -Verbose and -WhatIf parameters to have a dry run of the script without saving items to files.

Output is the following for SPE 6 and Sitecore with SXA module:

If you like this, share it.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: