Most useful Powershell scripts for a Sitecore developer
Here is a list of Sitecore PowerShell scripts that are useful for every Sitecore developer for making any bulk changes in Sitecore.
As the size of the Sitecore instance increases the maintenance of the Sites would become challenging and it brings the need for a scripting tool to make bulk actions on Sitecore items. Here is a list of such scripts which would do bulk activities like checking fallback, remove/updating workflows, organising the media items etc. Il try my best to add more to this list.
###1. Sitecore PowerShell script to enable Language fallback###
There may be a situation where you would like to make sure the language fallback is enabled on all your Sitecore items or a particular sub tree. Checking all the items manually is not a feasible task. In such cases this script can help to check if the language fallback is enabled and if required this script can enable the fallback setting and publish the item automatically in minutes/hours based on your instance size.
###2. Sitecore PowerShell script to remove all language versions except one###
For some reason you might want to remove all the language version except English or any other language. This would be tedious task to remove all language version even for one item as it requires to switching the language one by one and removing the versions. If there are multiple items, then it would be more challenging. So here is a script which can remove all the versions except one (either 'EN' or can be configured) for an item and its children recursively.
###3. Sitecore PowerShell script to remove/update workflows###
You might want to check if the right workflow is assigned to all of your items or you may have created a new workflow and like to assign this to all of your items in Sitecore or you simply want to remove the workflow of all your items. In any of these cases, you should be updating the workflow fields of each and every item in Sitecore. This is where this script can be used. This script can update the workflow field and workflow state field with a specific condition for all items or update only when there is a specific value conditionally.
###4. Sitecore PowerShell script to get unreferenced media items###
As your Sitecore instance gets older there could be more and more media items get added to the Sitecore media library. In the long term this impacts the size of Sitecore databases. Also, it could have a performance impact. It is always required to do some basic maintenance like removing/deleting the media items that are not used since a long time. This script gets the list of all unused/unreferenced media items from the media library.
###5. Sitecore powershell script to move media items based on references###
This powershell script would be really interesting. Say there are multiple websites in your Sitecore. All the media items for these Sites are unorganised and your content authors may place the images in one common folder, then it would be difficult to understand which images are being used in which website. So to solve this, we can take each image and check their references, based on the references it can be understood which site node is using this image. Now we can create a new folder inside the media library for this particular site and place this image in that folder. Like this we can check each of the media items inside the media library and move to the appropriate folder. If a media item is being used in multiple site nodes, then we can place this in a common folder. All these cannot be done manually and hence this script would be really helpful.
###6. Sitecore Powershell script to change template of items###
There may be a chance that some items could exist whose template may not be available, this only arises when we move the items to a difference Sitecore instance without including its template. In such cases, when we click on the item we would see a dialog with an error. It is not possible to correct it manually. The only option is to change the template of the item to an available template programatically. This script does the same. It changes the template id of the items to a selected template id.
###7. Sitecore Powershell script to remove renderings of items###
If you want to reset or remove the rendering of multiple items doing it manually would be a tedious task. As we have to remove each rendering one by one from the item presentation details. Doing this with a Sitecore Powershell script is the quickest solution.
8. Sitecore Powershell script to unpublish items
There might be a need to unpublish a node or some items under a node. Going to the item, disabling the publishable option and then publishing the item is not the right solution if we have multiple items. Instead we can have a Powershell script which can go through each of the item, disable the publishable option and publish the item automatically. This script does the same. It sets the Never publish option to '1' (equivalent to disabling the publishable option from Sitecore content editor) and publishes the item to web, which removes the item from web database.
9. Sitecore Powershell script to unlock all locked items in Sitecore
There could be chance that some of the items got locked by some content authors for editing and left like that. In the long run all such items can never be edited and has to be manually unlocked by some user with a administrator role. If you are a developer with an administer role, unlocking all such items manually is tedious task. Instead we can have a Powershell script which goes through each of the item and check if the item is locked and then unlocks it automatically. This is the script which unlocks all the items which are locked by any content authors.
I try my best to keep this list updated. Hope you find these scripts helpful. Please comment your thoughts.