Some useful SPE scripts

Sitecore Powershell Extensions (SPE) is a module that provide a command line and scripting environment as well as several new tools. It was developped by Adam Najmanowicz and Michael West and is for example a prerequisite to install Sitecore Experience Accelerator.

You can download this module from the marketplace or even from Sitecore’s download section.

When SPE is installed, you get following new items in your Sitecore dashboard

Powershell Toolbox is a collection of ready-made tools for various usages:

  • Create Anti-Package
  • Bulk Data Generator
  • Bulk Item Restorer
  • Bulk Template Changer
  • Data Importer
  • Find And Replace
  • Index Viewer
  • PowerShell Background Session Manager
  • Re-create site from sitemap
  • Rebuild script integration points
  • Logged in Session Manager
  • Transfer item security
  • SXA Site Manager
  • Task Manager

The Powershell Console is the console where you can write and execute your own Sitecore Powershell scripts.

Its documentation is very good, with some code snipplets.

Here are some easy and useful scripts to get you started and to show you how to use SPE

Unlock all items under the Content tree
Get-ChildItem master:\Content -Recurse | 
Where-Object { $_.Locking.IsLocked() } | 
% { $_.Locking.Unlock() }


List all children of an item (ex: content) and how many days ago they were modified
Get-ChildItem master:\Content -Recurse | 
Format-Table -auto Name, @{Label="Modified"; Expression={ [datetime]::Now.Subtract($_.__Updated).Days} }

List all children of an item who has been updated over the last week
Get-ChildItem master:\Content -Recurse | 
Where-Object { $_._Updated -gt [datetime]::Now.AddDays(-7) } | 
Format-Table -property DisplayName, "__Updated", "__Updated By", {$.Paths.Path}

Delete unused Media Items older than 1 month and who has no reference
Get-ChildItem -Path "master:\sitecore\media library" -Recurse |
Where-Object { $_.TemplateID -ne [Sitecore.TemplateIDs]::MediaFolder } |
Where-Object { $_._Owner -ne "sitecore\admin" -and $._Updated -lt [datetime]::Today.AddDays(-30) } | 
Where-Object { [Sitecore.Globals]::LinkDatabase.GetReferrerCount($) -eq 0 } |
remove-Item

Get all the users with an empty email in their profile
Get-User -Filter * | Where-Object { !$_.Profile.Email }

Set an email to a specific user (here sitecore\admin)
Set-User -Identity sitecore\admin -Email sitecore.admin@sigma.se


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: