Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Convers a Sitecore Powershell Extension item to a file on disk.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment