Tag Archives: PowerShell Script

Restart SharePoint 2013 Workflows with PowerShell

Recently I had an issue where my #SharePoint list #workflows wouldn’t run and were in suspended state. The reason for the suspended state was an item already existed in the destination folder where the workflow was supposed to move the file to.

I tried lots of tricks to get the workflows to automatically start, I had about 250 instances of this happening and no scripts I found online would resolve this issue. They just wouldn’t cancel / resume.

In the end I removed the workflow from the list (which killed all instances) and then republished. Perfect. Now how to start them all again? Hmm

Well because these are #SharePoint2013 Workflows that run on a workflow management server, the normal scripts for starting them are different. To cut it short, use the following #script and all should be good.

$sourceWebURL = ‘http://YourURL
$sourceListName = ‘YourListName
$TargetWorkflow = ‘YourWorkflowName
$spSourceWeb = Get-SPWeb $sourceWebURL
$spSourceList = $spSourceWeb.Lists[$sourceListName]
$items = $spSourceList.getItems()

# Getting a Workflow manager object to work with.
$wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($spSourceweb)
# Getting the subscriptions
$sub = $wfm.GetWorkflowSubscriptionService()
# Getting the specific workflow within the list of subscriptions on the specific list. (SP2010 associated workflows basically)
$WF = $sub.EnumerateSubscriptionsByList($spSourcelist.ID) | Where-Object {$_.Name -eq “$TargetWorkflow”}
# Getting a Workflow instance in order to perform my commands.
$wfis=$wfm.GetWorkflowInstanceService()

Foreach($item in $items){
# Creating the dictionary object I need to parse into StartWorkflow. This could be most other workflow commands.
$object = New-Object ‘system.collections.generic.dictionary[string,object]’
$object.Add(“WorkflowStart”, “StartWorkflow”);
$wfis.StartWorkflowOnListItem($WF, $item.ID, $object)
}

All you need to do is edit the BOLD text above and you should be on your way. Copy and Paste it into PowerShell ISE and save as PS1. Then run the file through the SharePoint 2013 Management Shell.

Good Luck!