[SP201x] Terminating runnning workflows with Powershell

After importing / restoring a site collection from a different environment, you might have the need to terminate all of the running workflows. For instance, to stop them from e-mailing people or firing off other events which should not be fired from within your local dev environment. Whatever reason you might have, here is a little Powershell script which can help you do so:

Add-PSSnapIn Microsoft.SharePoint.Powershell 

$url = "http://localhost:1234"

$web = Get-SPWeb $url 
$web.AllowUnsafeUpdates = $true;

try 
{
	$i = 0
	for ($i = $web.Lists.Count; $i -ge 0; $i--)
	{
		foreach ($item in $web.Lists[$i].Items)
		{
			foreach ($workflow in $item.Workflows)
			{
				if ($workflow.IsCompleted -ne $true)
				{
					[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($workflow)  
				}
			}
		}
	}

	foreach ($workflow in $web.Workflows)
	{
		if ($workflow.IsCompleted -ne $true)
		{
			[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($workflow)  
		}
	}
}
finally {
	$web.AllowUnsafeUpdates = $false
	$web.Update()
	$web.Dispose()
}

, , ,

Related posts

Latest posts

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *