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.

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.