Introduction:
Hello, this is Bing. Sitecore PowerShell Extensions (SPE) is a module that provides a command line and a scripting environment for automating tasks in Sitecore. You can use SPE to work with Sitecore items, modules, libraries and scripts. To run scripts in SPE, you need to have an elevated session state.
Source: Conversation with Bing, 26/2/2023
Purpose of this blog is to give some samples of how to use Powershell scripts. Sharing here hoping it will benefit someone like me someday..
You can start looking for examples from : https://doc.sitecorepowershell.com/working-with-items. Other links (1) Introduction – Sitecore PowerShell Extensions. Accessed 26/2/2023. (2) Libraries and Scripts – Sitecore PowerShell Extensions. Accessed 26/2/2023. (3) Scripting – Sitecore PowerShell Extensions. Accessed 26/2/2023.
Infact many of the following examples are collected from the above links and some other places like Stack overflow etc..
Note: Please note that these are not meant for maintenance so please refactor as needed.
Example 1: Get items by path of type “Product” filtered by language version “de-de”
{ItemPath} : e.g: /sitecore/content/CompanyName/Brand Sites/Tenant/Zone/CountryCode/Home/Products/
Get-childItem -Path master: "{ItemPath}/*[@@templatename='Product']" -Language de-de
Example 2: # Get items by path another way
#Get-ChildItem gets child items from the specified location
#Recurse : parameter is used to iterate through the tree descendants not limited to direct child of specified location
$items = Get-ChildItem -Path 'master:/sitecore/content/CompanyName/Brand Sites/Tenant/Zone/CountryCode/Home/Products/' -Recurse -Language de-de
Example 3: # Update the Page Designs of filtered items
$items = Get-ChildItem -Path 'master:/sitecore/content/CompanyName/Brand Sites/Tenant/Zone/CountryCode/Home/Products/' -Recurse -Language de-de | Where-Object { $_.TemplateName -eq 'Product' }
#Loop through the above items list
foreach($item in $items) {
#Write-Host command logs the message. here I am trying to log the item filed "Page Design" value along with another value item category
Write-Host $item.DisplayName "Page Design: " $item.Fields["Page Design"] "Category:" $item.Category
#command to enter into edit mode for the item
$item.Editing.BeginEdit()
#editing field value. here it is updating the item field value
$item.Fields["Page Design"].Value = "{XXXXXXXX-D991-4888-XXXX-5CD1XXXXXXX}"
# or a new value if changing it
$item.Category = "{XXXXECBE-8369-XXXX-95CB-768B24DXXXXX}"
#command to close edit mode for the item
$item.Editing.EndEdit()
Write-Host $item.DisplayName "Page Design: " $item.Fields["Page Design"] "Category:" $item.Category
}
Example 4: # Select-object to get the item properties in table format. At the end of the above script if you want to reverify whether items values updated or not run the following script
#to reverify the values are correctly updated using Select-object filter
Get-ChildItem -Path 'master:/sitecore/content/CompanyName/Brand Sites/Tenant/Zone/CountryCode/Home/Products/' -Recurse -Language de-de | Select-Object Name, DisplayName
Example 5: # Alternatively use the following Show-ListView
$items = Get-ChildItem -Path 'master:/sitecore/content/CompanyName/Brand Sites/Tenant/Zone/CountryCode/Home/Products/' -Recurse -Language de-de | Where-Object { $_.TemplateName -eq 'Product' }
$items| Show-ListView -Property @{Label="ID"; Expression={$_.ID} }, @{Label="Name"; Expression={$_.Name} }, @{Label="Path"; Expression={$_.Path} } }, @{Label="Category"; Expression={$_.Category} }
