Sitecore PowerShell Extensions: Installation, Sample Scripts, Advantages
Hello,
This blog gives details about Sitecore PowerShell Extensions, sample scripts, advantages etc.
What is Sitecore PowerShell Extensions ?
Sitecore PowerShell Extensions (SPE) is a Sitecore module which gives ability to execute PowerShell scripts in Sitecore client, use some out of the box features etc.
It is developed by Michael West and Adam Najmanowicz.
I will be using Sitecore.PowerShell.Extensions-6.4-IAR For Sitecore XP 10.3 for this demo.
Installation -
SPE is installed in the same way any content package will be installed in Sitecore. It comes as a .zip file and can be installed using Installation Wizard :
Download the compatible SPE version as per your Sitecore Product version from official link shown below:
For Sitecore 9.2 and SPE 6.2, if you are getting error like the following, then please change your App Pool Identity
Solution for above error can be found here:
Once it is correctly installed, you will see the following options
There are 2 ways you can execute PowerShell scripts via PowerShell Console or PowerShell ISE, here I will use PowerShell ISE for demo.
After clicking PowerShell ISE, you will see a Window asking for credentials for elevated permission and a predefined script as shown below :
Now, you can remove that script and execute your own script.
Useful Sample Scripts -
1) Example Script : Consider you are moving the fields from ABC template into XYZ template.
You want to get the field values of Items created from ABC template and store it in an Excel sheet.
Here, for demo, I have created a Template "State" having 2 fields "Capital City" and "Location Coordinates"
And Items created from above Template are "Karnataka" and "Maharashtra"
Use the following script to get field values, just change the path in $locationPath variable of the Item whose field values you want to copy.
# Connect to the Sitecore database
$sitecoreDb = "master"
# Location to retrieve information
$locationPath = "/sitecore/content/Home/Indian States/Maharashtra"
# Function to retrieve field values from an item
function Get-FieldValue {
param (
[Sitecore.Data.Items.Item]$item,
[string]$fieldName
)
$field = $item.Fields[$fieldName]
if ($field -ne $null) {
return $field.Value
} else {
return ""
}
}
# Get the location item
$locationItem = Get-Item -Path $locationPath -Database $sitecoreDb
# Create a custom object to store the field values
$itemInfo = [PSCustomObject]@{
"Capital City" = Get-FieldValue -item $locationItem -fieldName "Capital City"
"Location Coordinates" = Get-FieldValue -item $locationItem -fieldName "Location Coordinates"
}
# Output the field values
$itemInfo
in PowerShell ISE , it appears like this , click Execute
And output comes as follows:
(Note: If any Item is selected in the field then its raw value will come in output)
2) Example Script: Sitecore recommends an Item should not have more than 100 Children Items, similarly a Template should not have more than 50 fields for performance.
Recommended by LinkedIn
Use following script to find the number of fields a Template currently has, just change the path in $locationPath variable of the Template whose field count want to get:
# Connect to the Sitecore database
$sitecoreDb = "master"
# Location to retrieve information
$locationPath = "/sitecore/templates/Project/State"
# Function to retrieve field values from an item
function Get-ChildrenCount {
param (
[Sitecore.Data.Items.Item]$item
)
$childrenCount = Get-ChildItem -Path $locationPath -Recurse
return $childrenCount.Count
}
# Get the location item
$locationItem = Get-Item -Path $locationPath -Database $sitecoreDb
# Create a custom object to store the children count
$itemInfo = [PSCustomObject]@{
"Total Children" = Get-ChildrenCount -item $locationItem
}
# Output the field values
$itemInfo
you will get output like following:
(Note: Above script will include Field Section as child)
3) Use following script to get Items created from 14 days ago till today.
# Connect to the Sitecore database
$sitecoreDb = "master"
# Location to retrieve information
$locationPath = "/sitecore/content"
# Get the location item
$locationItem = Get-Item -Path $locationPath -Database $sitecoreDb
# Function to retrieve field values from an item
function Get-Items_Created_In_Last_14_Days {
param (
[Sitecore.Data.Items.Item]$item
)
# Get Today's Date and 14 days ago Date
$todaysDate = Get-Date
$startDate = $todaysDate.AddDays(-14)
$endDate = $todaysDate
$allCreatedItems = Get-ChildItem -Path $sitecoreDb -ID $item.ID -Recurse | Where-Object { ($_.__Updated).Date -ge $startDate -and ($_.__Updated).Date -lt $endDate} | Select-Object -Property @{Label="Path"; Expression={$_.Paths.FullPath}}
return $allCreatedItems
}
# Create a custom object to store the children count
$itemInfo = [PSCustomObject]@{
"All Created Items" = Get-Items_Created_In_Last_14_Days -item $locationItem
}
# Output the field values
$itemInfo
And PowerShell Output will be:
Advantages -
1) PowerShell is extremely powerful and versatile , it is very lightweight too.
2) SPE is known to accelerate the development process, it is one of the prerequisites for Installing Sitecore Experience Accelerator (SXA).
3) You can create a module as well or just use OOTB features, some are explained below :
a) You can copy roles from one user to other user with few clicks
b) Use Data importer / exporter to get Item's Data
c) To create package , no need to go to "Development Tools" => "Package Designer" .
If you are already browsing content tree, simply right click on the item to be included in package and follow the steps shown below
select required package options
4) You can write custom functions or even debug using SPE.
5) You can use it for Docker instance setup as well.
Note : Do not install SPE on Production CD servers as it has access to delete critical folders required for Operating System or Sitecore Ecosystem.
You can find more details on official Site for SPE
That's all folks, hope you like this article.
Please feel free if you want to suggest anything !
Enjoy Sitecore :)