Sitecore Powershell Extensions- Making work easier – Part 1

Hi all,

This time I am going to share a few powershell scripts that can be used for various requirements and reduce a big amount of time.

Before going on to those, first I would like to give some introduction about how I started scripting using SPE and how it helped me to proceed further.

So this was before 2 years, when I was working in a project and it was using Zen Garden, The first ever Sitecore Development Accelerator (Now Sitecore has stopped supporting the same and is discontinued) which had a similar properties as of Sitecore Experience Accelarator (SXA), which was used to created Multi-Sites using Tenants, Markets etc. So it was told that when we just right click on an item and select “Create a Tenant”, it showed some dialog boxes asking for templates, site name , language etc. and when given the necessary details, the site node was created immediately. There I was curious on how this is happening and after some research and debugging, I came to know that these all could be possible only because of Sitecore Powershell Extensions. There started my first step to SPE.

The Sitecore PowerShell module is especially handy when you’ve been tasked with applying changes to items in bulk. Then I started working on various scripts for different tasks that requires time saving and easy to be automated and was happy to know that it was working and still I am continuing my PowerShell scripting journey.

There are a large number of blogs for studying and implementing SPE. Of those i am suggesting following blogs to go through the basics of the same

Here I would like to share some scripts that can be handy on some occasions. Some of these scripts are also available in other websites also and these scripts might not be the only solution for the tasks but you could also try these scripts also as part of your investigation.

This blog post is heavily inspired from the posts and help provided by Adam Najmanowicz and Michael West on Sitecore PowerShell Extensions, through their blogs, Slack, Stack overflow etc. I have seen both of them working and helping really a lot for the people who are facing any difficulties in implementing the same. For joining the group which helps you to interact with Adam Najmanowicz and Michael West and more Powershell experts, please join the Sitecore Slack channel.

Now lets move on to the scripts one by one.

1. To change the template of all the child items

$path="Item path here"
$item = Get-ChildItem -path "$($path)"
$newTemplate = [Sitecore.Configuration.Factory]::GetDatabase("master").Templates["Template path here"];
$item | ForEach-Object {
    if($_.TemplateName -eq "TemplateName")

2. To delete the sub item of all the child items of main item

$path="item path here"
$item = Get-ChildItem -path "$($path)" -Recurse
$item | ForEach-Object {
    if($_.TemplateName -eq "TemplateName")
        Get-ChildItem $_.ProviderPath | Remove-Item

3. Export the list of item under a particular item excluding a particular template to csv

$path="item path here"
$item = Get-ChildItem -path "$($path)" -Recurse | Where-Object { $_.TemplateName -ne "Template Name of items to be exported" }
$item | Show-ListView -Property `
    @{ Name="Item Name"; Expression={$_.DisplayName}}, # property renamed
    @{ Name="Item path"; Expression={$_.FullPath}}

4. To create items under an item from csv

$itempath = "item path here"
$templatePath = "template path here"
$InputcsvFile = Show-Input "Input file path"
$csv = Import-CSV $InputcsvFile -delimiter ","
$bulk = New-Object "Sitecore.Data.BulkUpdateContext"
	foreach($record in $csv)
		$item = New-Item -Path $itempath -Name $record.ItemName -ItemType $templatePath
    		$item["__Display name"] = $record.ItemName 
		Write-Host "Item created: " $record.ItemName

5. To get all the links starting with ‘a’ tag in the items (from a richtext editor or single line text)

$itempath = "item path here"
$allItems = Get-ChildItem -Path $itempath -Recurse -language * -version *
$links = @() 
$allItems | ForEach-Object {
    foreach($field in $_.Fields) {
    # Only look within Single-line and Rich Text fields     
    if($field.Type -eq "Single-Line Text" -or $field.Type -eq "Rich Text") {
            $html = $field.Value
            $htmlDocument = New-Object -TypeName HtmlAgilityPack.HtmlDocument
            foreach($x in $htmlDocument.DocumentNode.SelectNodes("//a")) {                
               $links +=@{Id=&{$};Path=&{$_.paths.fullpath};Language=&{$_.language};Version=&{$_.version};Link=&{$x.OuterHtml}}                
$links | Show-Listview -Prop Id,Path,Language,Version,Link

These were a few scripts which i think will be useful for someone who is curious to learn powershell and use them for reducing the amount of time in their projects.

Please feel free to contact me if any of you face any issues or having doubts in using the scripts.

I will continue with some new scripts in my upcoming blogs.
Till then, See you all. Happy Scripting …


One thought on “Sitecore Powershell Extensions- Making work easier – Part 1

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