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.
- #Provide the Site collection url
- $web = Get-SPWeb "http://shareserver.com/sitecollection";
- $web.AllowUnsafeUpdates = $true;
-
- #Provide the List or Library name
- $list = $web.Lists["SD"];
- $count = 0
-
- #Loop through all Items in List/Library
- foreach ($listItem in $list.Items)
- {
- #Loop through all Workflows on each List Items/Documents.
- foreach ($workflow in $listItem.Workflows)
- {
- #Ignore Completed 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();