In the previous post, we saw how to use Get-Item and GetChildItem to retrieve information about Sitecore items. These are useful when we want every item that fits under the given Path to be retrieved. Now, let’s discuss more about what problems can be solved using Where-Object.
Get-ChildItem cmdlet retrieves a list of children of a given node. We can also use –Recuse to get all of its descendants. What if we want to add a filter or provide a condition to filter out items that we do not want in our result?
We can achieve this by using Where-Object cmdlet separated by Pipe (|). Following is the statement of how we can filter our result set based on TemplateName where TemplateName is Event Details.
Get-ChildItem -Path master:\content\events -Recurse | Where-Object { $_.TemplateName -eq "Event Details"}
This will filter and return a result that contains items created from Event Details template only. We can pass our filter condition inside a script block. Here we are using -eq operator.
You will notice that we are using $_.TemplateName. Here, $_ is nothing but a token that is used to refer to items inside a loop or here each item inside our result set.
It works similar to C# Lambda Expression in Linq.
For Example, $_ is same as i in i => i.Name.Contains(“Events”)
Now, after executing the above statement, we get the following filtered result:
See the TemplateName column. We only got the entries that have Event Details in TemplateName. We have used -eq in our condition, but there are many other useful conditional cmdlets that we can use in Where-Object. Lets see some of them.
-ne Operator
This is “not equal to” the operator that we can use in our Condition script block of Where-Object. Following is the statement to include items in the result set which are not created from Event Details template.
Get-ChildItem -Path master:\content\events -Recurse| Where-Object { $_.TemplateName -ne "Event Details"}
Output for above:
You can see that the output list does not contain any items that have Event Details in TemplateName.
-Like
This operator can be used to match a value using wildcards where we can specify Starts with, Ends with, or contains type conditions.
Following is a statement which returns the list of items that has “Event” as substring in TemplateName property.
Get-ChildItem -Path master:\content\events -Recurse| Where-Object { $_.TemplateName -like "*Event*"}
Output of the above statement:
-gt Operator
This is Greater than operator (>). We can use this to compare numerical values. I used it to compare DifficultyLevel field of Event Details items. Following is the statement:
Get-ChildItem -Path master:\content\events -Recurse| Where-Object { $_.DifficultyLevel -gt 3}
Executing the above statement will return the result containing items that have DifficultyLevel field value greater than 3.
Output of above statement:
You can see that the list returned has only two items. Let’s include DifficultyLevel Field in our output, which is a numeric field. This time, we will not be using Format table. Instead, we will use Select-Object to select only specific fields to display.
The main feature of using Select-Object is that we can show calculated values in our result set but here we will only be using it to display out selected field.
Following is the modified statement with Select-Object:
Get-ChildItem -Path master:\content\events -Recurse | Where-Object { $_.DifficultyLevel -gt 3} | Select-Object -Property Name, DifficultyLevel, TemplateName
Output of the above statement:
You can see in the image that those two results have DifficultyLevel field value as 4 and 5 which are greater than 3.
Note: Less than operator -lt works opposite as it is less than operator.
Logical operators -And and -Or
We can use logical operator And and Or in our Where-Object conditions. Following is the statement that uses them. This statement will return a result containing items that are not created from Event Details template nor from Event Lists template.
Get-ChildItem -Path master:\content\events -Recurse| Where-Object { $_.TemplateName -ne "Event Details" -and $_.TemplateName -ne "Event List"}
Output of the above:
In this post we saw how to filter our result set using Where-Object. Till now, we were using normal commands to get item information. These commands fetch data from Sitecore Databases. In the next post, we are going to see how we can query Sitecore indexes using ContentSearch API.
Great Blog! A very detailed description of the results and the process!
Thank you Jacob.