Cancel All Running Workflows For All List Items/ Documents In A List/ Library

 There may be some scenario where we need to cancel all the running workflows for a List/Library. In that case we can use the following PowerShell script to achieve it.

  1. #Provide the Site collection url  
  2. $web = Get-SPWeb "http://shareserver.com/sitecollection";   
  3. $web.AllowUnsafeUpdates = $true;      
  4.  
  5. #Provide the List or Library name   
  6. $list = $web.Lists["SD"];  
  7. $count = 0  
  8.  
  9. #Loop through all Items in List/Library   
  10. foreach ($listItem in $list.Items)   
  11. {  
  12.     #Loop through all Workflows on each List Items/Documents.           
  13.         foreach ($workflow in $listItem.Workflows)   
  14.     {  
  15.         #Ignore Completed Workflows   
  16.         if(($listItem.Workflows | where   
  17.         {$_.InternalState -ne "Completed"}) -ne $null)  
  18.         {  
  19.              
  20.             #Cancel Workflows          
  21.             [Microsoft.SharePoint.Workflow.SPWorkflowManager]::  
  22.                     CancelWorkflow($workflow);        
  23.             write-output "Workflow cancelled for :   
  24.                     " $listItem.Title;    
  25.         }  
  26.     }  
  27. }  
  28. $web.Dispose();