SharePoint 2013 – Workflow Management – Starting a workflow using Powershell
Introduction
As it is right now, the SharePoint 2013 workflow management isn't exactly straight up using Powershell. The below code should aid in the understanding on how to work with it in its current state.
The solution
$sourceWebURL = '<URL>'$sourceListName = '<List Name>'$TargetWorkflow = '<Workflow Name>'$spSourceWeb = Get-SPWeb $sourceWebURL$spSourceList = $spSourceWeb.Lists[$sourceListName] #-- 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 $spSourceList){ #-- 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)
References
Microsoft.SharePoint.Client.WorkflowServices namespace
Additional credit
Frej Laursen, Joachim Bach & Per Jakobsen.