In my last post, I gave an overview of Sitecore Powershell and how to install the Sitecore Powershell Extension. Sitecore Powershell can also be used to work with Sitecore items. In this blog, we will see how we can use it to retrieve information about items in our content tree. We would be using Get-item and Get-ChildItem cmdlets.
A cmdlet is a lightweight command that is used in the PowerShell environment. It acts as a lightweight Powershell script that performs a specific action.
Below is the image of how my site structure looks:
Now, let’s open the Powershell ISE and start to create our script.
Using Get-Item cmdlet
- Used to retrieve a single item.
- We will be using -Path, -Language and -Version
- -Path is used to specify the path of the item that we want to retrieve.
- -Language is used to specify which language version of the item we want to retrieve.
- -Version is used to specify which numbered version of an item we want to retrieve.
Get Information About A Single Item Using Get-Item
Write the Get-Item command as mentioned below and pass your item path, as I have passed the path to my events item.
Get-Item -Path "master: \content\events"
After you have written this command and specified the path to your item, click on the Execute button in the toolbar. Following is the output that I got after executing.
Note: You will need elevated access/login to execute scripts.
Get Language-Specific Version of an Item
We can use –Language to specify which language version of a particular item we want to retrieve. Following is the command that I used to get the German version of my events item.
Get-Item -Path master: \content\events -Language de-DE
I can see the below information after execution.
You can see that I am getting both the English version and the German version of my events item. This is because Execute works on the whole script and not just one statement. So, it will execute both our statements.
If we want only our second statement to execute, we need to select the statement and use the Selection button to execute it. Using the Selection button produced the below output:
Get Specific Numbered Version of an Item
We can use –Version for specifying which numbered version we want for an item. Specify the number after –Version. Following will retrieve version 2 of our events item:
Get-Item -Path master: \content\events -Version 2
Macro Trends in Sitecore and DXP
Over the past few years, Sitecore has transformed its architecture, offerings, and vernacular. The DXP landscape is evolving and organizations are increasingly embracing these changes. This guide explores six emerging trends in Sitecore and the DXP landscape.
The output is as below:
Get all Language and Numbered Versions of an Item
We can use * after both –Language and -Version in our command to get all language or numbered versions. We can also use both –Language and -Version in our command at the same time to get every version of our item:
Get-Item -Path master: \content\events -Language * -Version *
Below is the output for executing the above command:
Get Items Using ID
You can also fetch an item by passing ID in Get-Item. Following is the syntax to do it:
Get-Item -Path master: -ID “SpecifyYouIDHere”
Following is the output when I specify ID of my Home Page item:
Using Get-ChildItem cmdlet
We can use Get-ChildItem to get a list of child items of a node that we will specify using –Path. Following is the command that will retrieve child items of my events node.
Get-ChildItem -Path master: \content\events
Following is the output after execution:
You can see that it only retrieved first-level children of Events node. If we want a list of an item’s children and grandchildren, we need to add -Recurse. This will retrieve children recursively.
Get-ChildItem -Path master: \content\events -Recurse
Below is the output:
Format the Output View
We can format the output to include the information that we want. We will be using Pipe (|) for achieving this. We use | to specify that result of the first statement will be used as input for the second one that comes after | character.
We will be formatting our output to include DisplayName, Creation timestamp, TemplateName, and ContentHeading field. Following is the command to do that:
Get-ChildItem -Path master:\content\events | Format-Table -Property DisplayName, ContentHeading, Created, TemplateName
See how we have used Pipe (|) in the above command. It works like this – Use the output of Get-ChildItem cmdlet before | as input to Format-Table cmdlet.
So, the children list of Events node is used as input in Format-Table command which use -Property arguments to filter the fields that will be included in the output table.
Below is the output after executing the above command:
Count the Objects in the Result Set
We can use Measure-Object to get the total count of objects in the returned result set. Using the following statement, I can get the count of items in our result set:
Get-ChildItem -Path master:\content\events -Recurse| Measure-Object
In this article, we have seen how to fetch a particular item using path or ID, how to fetch a list of children, and how too format output according to our needs. In the next blog, we will be exploring about accessing fields and looping through each item in the Children list.
Learn more about Sitecore from our blog.