Sitecore Powershell Extension is a great tool/module developed by Adam Najmanowicz and Michael West that provides a command line and scripting environment for automating tasks.
In my previous blog post on Sitecore Powershell extensions, we learned very basics of this great module, like it’s introduction, how to install this module, and it’s features.
I would like to take another step in continuation of previous blog post, and like to show some features which can be leveraged out of it.
Retrieve Sitecore Item
We can fetch Sitecore Item based on ID or Path:
If we know the Sitecore item ID, it can be retrieved in following way:
We have to make sure that we are adding the context for database, in this case I have added “master”
If we know the Sitecore item path, it can be retrieved in following way:
Retrieve Items from all languages and versions
We just need to pass language and version parameter, if we want items based on specific language or version, we can specify that language and version as parameter values, instead of passing * to it.
Retrieve child Items
We can get all child items based on Sitecore path, this is how it can be used:
If you want to retrieve all descendants from the given path, we can add recurse parameter, please see following for ref:
There can be scenarios, where certain field(s) are not updated or empty for some reasons, and now you want to update that specific field for all the items, this is now not so easy, as you have several hundreds of items, but,there is an easy way to make this bulk update using powershell extensions.
In the above example- we are trying to filter all the child items under /content/helix/home node where “Browser Title” field is empty, once we have a list of all items, we are iterating over all such items and updating the field with the “Display Name” value of the item.
Bulk updates for “Alt” text
Another very valid scenario for bulk updates can be updating all the Image items where “Alt” text is missing, following can be used to update all such Image items:
In the above example- we have used Sitecore query to get all the items based on template name “Image” and then finding such items which has missing “Alt” text, once we have a list we just updated the “Alt” text with item display name value, and then it can be updated by content authors to put something more specific, if required.
I strongly encourage all Sitecore developers to start using this module, and see how this can be leveraged into your solution, this is for sure helping and going to help everyone a lot.
I will continue sharing my thoughts and experiences on this topic, please let me know if you have any questions.
Happy learning 🙂