PowerShell to Cancel All SharePoint Workflows in Progress

I was struck with requirement were having big list and workflow attached to it, need to cancel all running workflows. 
It could be either manually traverse all the way and stop it, was next to impossible.
Following script helped me to save my time!


$web = Get-SPWeb "http://mysharepointserver.com/subsite";
$web.AllowUnsafeUpdates = $true;    

$list = $web.Lists["CustomListName"];
$count = 0

foreach ($listItem in $list.Items) 
{
 foreach ($workflow in $listItem.Workflows) 
 {
 
  if(($listItem.Workflows | where 
  {$_.InternalState -ne "Completed"}) -ne $null)
  {
   #Cancel Workflows        
   [Microsoft.SharePoint.Workflow.SPWorkflowManager]::
     CancelWorkflow($workflow);      
   write-output "Workflow cancelled for : 
     " $listItem.Title;  
  }
 }
}
$web.Dispose();
Hope this will save your time!

Comments

Popular posts from this blog

Search Query by ContentType name issue

SharePoint 2013 List CRUD operation using Angular and REST API

Search Result Webpart,Filter result If QueryString is present in URL