How to cancel SharePoint workflows using PowerShell?

[Verified]: Cancel SharePoint workflows using PowerShell

One comment

 7,429 total views,  2 views today

In this tutorial, we will learn about how to cancel SharePoint workflows using the PowerShell script.

Business Scenario:

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 running state however, 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.


#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:

Thus, in this article, we have learned about how to cancel SharePoint workflows using the PowerShell script.

See Also:

You may also like the following SharePoint PowerShell tutorials:

Download SharePoint Online & Office 365 Administration eBook

Buy the premium version of SharePoint Online & Office 365 administration eBook from here:



Buy SharePoint Online & Office 365 Administration eBook


 

Get the free demo PDF eBook from here:

FREE DOWNLOAD

Send download link to:

Subscribe to get exclusive content and recommendations every month. You can unsubscribe anytime.

 


 

 

1 comments on “[Verified]: Cancel SharePoint workflows using PowerShell”