SharePoint Online: Terminate Workflow using PowerShell

Requirement: Stop workflow in SharePoint Online using PowerShell

Terminate Workflow in SharePoint Online using PowerShell

SharePoint Online: PowerShell to Cancel Workflow

Let’s terminate all workflow instances of a specific workflow in all items of a list using PowerShell.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"

#Set Parameters
$ListName ="Project Tasks"
$WorkflowName ="Send Email"

#Get Credentials to connect
$Cred= Get-Credential

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    #Get the Web, List and List Item Objects
    $Web = $Ctx.Web
    $List = $Web.Lists.GetByTitle($ListName)
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())

    #Initialize Workflow Manager and other related objects
    $WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($Ctx, $Web)
    $WorkflowSubscriptionService = $workflowServicesManager.GetWorkflowSubscriptionService()
    $WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService() 
    $WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)

    #Get the Target workflow to Terminate
    $WorkflowAssociation = $WorkflowAssociations | Where {$_.Name -eq $WorkflowName}
    #Loop through each List Item
    ForEach($ListItem in $ListItems)
        #Get Workflow Instances of the List Item            
        $WorkflowInstanceCollection = $WorkflowInstanceService.EnumerateInstancesForListItem($List.Id, $ListItem.Id) 
        #Filter All Workflows Instances of the target workflow where Status is: Started or Canceled or Suspended
        $WorkflowInstancesToKill = $WorkflowInstanceCollection | Where { ($_.Status -eq "Started" -or $_.Status -eq "Canceled" -or $_.Status -eq "Suspended") `
                                                                 -and $_.WorkflowSubscriptionId -eq $WorkflowAssociation.Id}
        #Terminate Workflow Instances
        ForEach ($WorkflowInstance in $WorkflowInstancesToKill) 
                #To Cancel workflow, use: $WorkflowInstanceService.CancelWorkflow($WorkflowInstance)
                Write-host -f Green "Worfklow Terminated on List Item:" $ListItem.Id
            } Catch {
                     Write-host "Error terminating workflow on " $ListItem.Id " Error Details: $_"
Catch {
Write-host -f Red "Error:" $_.Exception.Message

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply