How to terminate multiple instances of the same workflow

  • 15 February 2022
  • 0 replies
  • 486 views

Userlevel 3
Badge +8

Topic

How to terminate multiple instances of the same list workflow

 

Instructions

Copy the below PowerShell script and save as a ps1 file. Execute the ps1 file and you should be prompted to enter information retaining to the location of the workflow you want terminated. This script will terminate all instances of the list workflow by workflow name.

---------------------------------------------------------------------------------------------------------
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue
function Cancel-SPWorkflow(){
PARAM 
(
[Parameter(ValueFromPipeline=$true)] [Microsoft.SharePoint.Workflow.SPWorkflow] $SPWorkflow
)

BEGIN {
  }

END {
}

PROCESS {
        [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($SPworkflow)
    }
}
function Get-SPWorkflow(){
PARAM 
(
[Parameter(ValueFromPipeline=$true)] [Microsoft.SharePoint.SPListItem] $SPListItem
)

BEGIN {
  }

END {
}

PROCESS {
        $SPListItem.Workflows
    }
}
$siteurl = Read-Host "Enter the site URL"
$web = Get-spweb $siteurl
$listname = Read-Host "Enter the name of the list"
$workflowname = Read-Host "Enter the name of the workflow"
$targetlist = $web.Lists["$listname"]
$WFA = $targetlist.workflowassociations | WHERE {$_.Name -eq $workflowname} 
$WFID = $wfa.id 

$choice = ""
 while ($choice -notmatch "[y|n]"){
     $choice = read-host " Are you sure want to cancel all workflow instances of $workflowname from $listname ? (Y/N)"
     }
 
if ($choice -eq "y"){

$(Get-SPWeb $siteurl).Lists[$listname].Items | Get-SPWorkflow | where {[String]$_.AssociationID -match [String] $WFID} | Cancel-SPWorkflow

Write-host "All instances of" $workflowname "workflow have been cancelled."

}

else {write-host "Please correct the information."}

 


0 replies

Be the first to reply!

Reply