Home » Content Editor » Expand Tokens on Sitecore Items Using a PowerShell Function in Sitecore PowerShell Extensions

Expand Tokens on Sitecore Items Using a PowerShell Function in Sitecore PowerShell Extensions

Sitecore Technology MVP 2016
Sitecore MVP 2015
Sitecore MVP 2014

Enter your email address to follow this blog and receive notifications of new posts by email.

During my Sitecore from the Command Line presentation at the Sitecore User Group – New England, I had briefly showcased a custom PowerShell function that expands Sitecore tokens in fields of a supplied item, and how I had saved this function into the Functions section of the Script Library — /sitecore/system/Modules/PowerShell/Script Library/Functions — of the Sitecore PowerShell Extensions module. This blog post captures what I had shown.

This is the custom function I had shown — albeit I changed its name to adhere to the naming convention in PowerShell for functions and commands (Verb-SingularNoun):

function Expand-SitecoreToken {
             Expand tokens on the supplied item
            Expand tokens on the home item.
            PS master:\> Get-Item "/sitecore/content/home" | Expand-SitecoreToken
        $tokenReplacer = [Sitecore.Configuration.Factory]::GetMasterVariablesReplacer()
        $result = $item.Editing.EndEdit()
        "Expanded tokens on item " + $item.Paths.Path
    Catch [system.Exception]
        "Failed to expand tokens on item"
        "Reason: " + $error

The function above calls Sitecore.Configuration.Factory.GetMasterVariablesReplacer() for an instance of the MasterVariablesReplacer class — which is defined and can be overridden in the “MasterVariablesReplacer” setting in your Sitecore instance’s Web.config — and passes the item supplied to the function to the MasterVariablesReplacer instance’s ReplaceItem() method after the item has been put into editing mode.

Once tokens have been expanded, a confirmation message is sent to the Results window.

If an exception is caught, we display it — the exception is captured in the $error global variable.

I saved the above function into the Script Library of my copy of the Sitecore PowerShell Extensions module:


An item was created in the Script Library to house the function:


Let’s try it out.

Let’s expand tokens on the Home item:


In the Integrated Scripting Environment of the Sitecore PowerShell Extensions module, I typed in the following code:

Execute-Script "master:/system/Modules/PowerShell/Script Library/Functions/Expand-SitecoreToken"
Get-Item . | Expand-SitecoreToken

You can consider the Execute-Script “master:/system/Modules/PowerShell/Script Library/Functions/Expand-SitecoreToken” line of code to be comparable to a javascript “script” tag — it will execute the script thus defining the function so we can execute it.

I then ran that code above:


Once the script finished running, I went back over to the Content Editor, and saw that tokens were expanded on the Home item:


You might be thinking “Mike, I really don’t want to be bothered with expanding these tokens, and would rather have our Content Editors/Authors do it. is there something we can set up to make that happen?”

You bet. 🙂

In the Sitecore PowerShell Extension module, you can save PowerShell into the Script Library to be executed via an item context menu option click. All you have to do is save it into the Script Library under the Content Editor Context Menu item:


The script is then saved in a new item created under Content Editor Context Menu item in the Script Library:


Let’s see it in action.

I chose the following page at random to expand tokens:


I right-clicked on the item to launch it’s context menu, opened up scripts, and saw a new “Expand Tokens” option:


I clicked it, and was given a dialog with a status bar:


I refreshed the item, and saw that all tokens were expanded:


If you have any thoughts or questions on this, please leave a comment.

Until next time, have a scriptabulous day!



  1. Michael West says:

    Great article! I had not realized it was that easy to add the context menu entry.


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 )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: