Context Menu PowerShell Script to delete items with a specific substring within a bucket

Recently, I came across a query in the Sitecore Slack Channel where someone had asked about a way to delete items with a specific substring within a bucket using Sitecore PowerShell - This is the content for this blog article. 

In this blog article, I add a context menu that will appear on the right click of a Sitecore Item and on clicking the concerned context menu item, a PowerShell Script will run to find if a specified string, in this case, I narrowed the scope to "new" is present and so, items with "new" in the title will be deleted from the bucket. So, here is the step-by-step approach:

1. Ensure you have PowerShell Extensions installed. I used Sitecore Complete Install Assistant (SCIA) to install Sitecore 10.0.1 (SCIA by default installs SPE).

2. Once Sitecore / PSE is installed, you can verify the same in the launchpad:


3. Let's insert Module Wizard in /sitecore/system/Modules/PowerShell/Script Library:



4. Give a relevant name. I named my module as Item Manager Module. Since I want the module to be available in the context menu (while right clicking an item in the content tree), I selected Context Menu in the list and pressed Proceed button:


You can choose other options like adding a button to ribbon apart from others.

5. Once the module is successfully added, you should see it in the Sitecore Content tree:




6. On click of the Context Menu, we need a PowerShell Script to execute, right click and add the same:



7. I named the script item as Delete Item and I can see it in the Content tree once added:



8. Next, Click on Elevate Session, provide Sitecore Admin password and press Ok button:



9. You should now see more fields in the item to add script body apart from others:


10. Change the icon for the Delete Item to a relevant one - 



11. Next, let's add the PowerShell Script to the Script body field:


12. Here are the most important parts of the PowerShell Script that's used for the purpose of deletion:

    12.1 $startPath = $_.Paths.FullPath

    - Gets the full path of the item on which the Delete Item context menu is clicked.

    12.2 $itemsToProcess = Get-ChildItem $startPath -Language * -Recurse

    - Gets all the children of the item selected in 12.1

    12.3 if($itemsToProcess -ne $null) 

    - Checks if the child item is not null

    12.4   $itemsToProcess | ForEach-Object  

    - Starts Processing each item, one at a time
       
    12.5        foreach($field in $_.Fields) 

    - Gets each field in the concerned child item

    12.6             if($field -match '.*new.*') 

    - Checks if the item title has "new" in it

        Note: Change the condition to look like this in case if you need to get items that start with "new"                  if($field -match '^new')  and if you just have to check if the string ends with "new", then this                 is the way to do - if($field -match 'new$')
                    
    12.7                   $_.Paths.FullPath | Remove-Item

    - Deletes the item 

13. I also use the PS ISE from the Launch Pad to interactively debug the script:


You can debug variables (you use in the script) like $field in this script using Write-Host to adjust the script as needed.

14. So, here is a quick video as to how the script gets invoked when you right click a bucket list:


Complete PS Script here - https://raw.githubusercontent.com/navancommits/sitecore10/master/Delete_Item_with_Specific_Substring_PS_Script.ps1
       
A more optimized approach to search is here - https://stackoverflow.com/questions/37484216/sitecore-powershell-search-all-the-items-which-contain-the-keyword

References:
https://www.logicalfeed.com/posts/1222/powershell-script-to-search-a-keyword-in-all-items-in-sitecore
https://sitecoresandbox.com/2016/06/03/content-editor-ribbon-buttons-using-sitecore-powershell-extensions/    

Comments