Sunday, December 8, 2013

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!

No comments:

Post a Comment