In this SharePoint Workflow tutorial, we will learn about how to cancel SharePoint workflows using the PowerShell script.
Business Scenario: SharePoint Workflow
Your farm has lots of sites and each site has lots of list workflows, there could be scenarios where we could see there are many workflow instances that are in a running state however, the status is error out or there are many long-running workflow instances that are running over the years, for both the scenario if we try to cancel those workflows which will be a hectic task. So, we can automate this process by running this script which will cancel all running workflows in SharePoint or it will check the error out workflow then the script will cancel those said workflows.
Cancel SharePoint workflow using PowerShell
#The below script is used to cancel all error out running workflow using the PowerShell script. cls $PSshell = Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorVariable err -ErrorAction SilentlyContinue if($PSshell -eq $null) { Add-PSSnapin "Microsoft.SharePoint.PowerShell" } $fileName = "CancelWorkflowReport" #'yyyyMMddhhmm yyyyMMdd $enddate = (Get-Date).tostring("yyyyMMddhhmmss") #$filename = $enddate + '_VMReport.doc' $logFileName = $fileName +"_"+ $enddate+"_Log.txt" $invocation = (Get-Variable MyInvocation).Value $directoryPath = Split-Path $invocation.MyCommand.Path $directoryPathForLog=$directoryPath+"\"+"LogFiles" if(!(Test-Path -path $directoryPathForLog)) { New-Item -ItemType directory -Path $directoryPathForLog #Write-Host "Please Provide Proper Log Path" -ForegroundColor Red } #$logPath = $directoryPath + "\" + $logFileName $logPath = $directoryPathForLog + "\" + $logFileName $isLogFileCreated = $False function Write-Log([string]$logMsg) { if(!$isLogFileCreated){ Write-Host "Creating Log File..." if(!(Test-Path -path $directoryPath)) { Write-Host "Please Provide Proper Log Path" -ForegroundColor Red } else { $script:isLogFileCreated = $True Write-Host "Log File ($logFileName) Created..." [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg) Add-Content -Path $logPath -Value $logMessage } } else { [string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg) Add-Content -Path $logPath -Value $logMessage } } #######The below function cancel all running workflow from the given site and list/library.############################ function CanCelAllWorkflow() { param ( [Parameter(Mandatory=$true)] [string] $SiteURL, [Parameter(Mandatory=$true)] [string] $ListName ) #Site URL $myWeb = Get-SPWeb $SiteURL #List Name $myList = $web.Lists[$ListName] # Iterate through all Items and all Workflows on Items foreach ($oneItem in $myList.Items) { foreach ($wf in $oneItem.Workflows) { #Cancel Workflows [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf) } } } #######The below function cancel all running workflow from the given site and list/library based on the error condition.############################ function CancelAllErrorOutWorkflow() { param ( [Parameter(Mandatory=$true)] [string] $SiteURL, [Parameter(Mandatory=$true)] [string] $ListName ) #Site URL $myWeb = Get-SPWeb $SiteURL #List Name $myList = $myWeb.Lists[$ListName] # Iterate through all Items and all Workflows on Items foreach ($oneItem in $myList.Items) { foreach ($wf in $oneItem.Workflows) { if($wf.InternalState -match 'Error') { #Cancel Workflows [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($wf) } } } } ####################Testing - calling the function######################################################## try { CancelAllErrorOutWorkflow "https://yoursiteURL" "YourListName" $message="The CancelAllErrorOutWorkflow execution has been completed successfully." Write-Host $message -BackgroundColor Green } catch { $ErrorMessage = $_.Exception.Message +"in the CancelAllErrorOutWorkflow execution script!: " Write-Host $ErrorMessage -BackgroundColor Red Write-Log $ErrorMessage } ####################Testing - calling the function ends here####################################
Summary: Cancel SharePoint Workflow using PowerShell
Thus, in this article, we have learned about how to cancel SharePoint workflows using the PowerShell script.
See Also: SharePoint Workflow
You may also like the following SharePoint PowerShell tutorials:
- SharePoint Online: Remove custom app using PnP PowerShell
- Office 365: How to create content type in SharePoint Online using PowerShell?
- Office 365: How to create document library in SharePoint Online using PowerShell?
- Export SharePoint user information list to CSV(Excel) file using PowerShell
- How to fix “The term ‘Get-MsolUser’ is not recognized as the name of a cmdlet”
- How to fix the “The term ‘Get-SPWeb’ is not recognized as the name of a cmdlet, function” PowerShell error
- How to hide quick launch menu in SharePoint online using PnP PowerShell
- Edit user Permission is greyed Out SharePoint Online
- Get workflow inventory from SharePoint online using PowerShell CSOM
- Create a modern team site using PnP PowerShell in SharePoint
- In 2 steps convert a classic SharePoint page to modern using PnP
- SharePoint Online: Delete All Files from document library for the given date – PowerShell CSOM
- Create SharePoint online list using PnP provisioning template
- SharePoint Automation: PowerShell script to get remote server information
- Office 365: Retrieve hub sites and associated sites using PnP Powershell
- SharePoint Online Automation – O365 – Upload files to document library using PowerShell CSOM
- SharePoint Online Automation – O365 – Create multiple items in a list using PowerShell CSOM
- SharePoint Online Automation – O365 – Update document library metadata using PowerShell CSOM
- Creating a workflow by using SharePoint Designer 2013 and the SharePoint Workflow platform

1 comments on “SharePoint Workflow: Quickly cancel SharePoint workflows using PowerShell”