Start a workflow for all items in a list via PowerShell

In researching this I found a lot of incomplete or inaccurate information in blogs and message boards. What I am trying to do is simple, start an existing workflow for each item on a custom list via PowerShell. I save this PowerShell script to my SharePoint App server’s D: drive and start it on a schedule via the task scheduler. Here is the working PS script…

Add-PSSnapin “Microsoft.SharePoint.PowerShell”

$sourceWebURL = ‘URL of site’
$sourceListName = ‘List Name’
$TargetWorkflow = ‘Name of Workflow’
$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()

$spSourceListItems = $spSourceList.Items
Foreach($item in $spSourceListItems){
#Creating the dictonary 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)
}

This blog post was the closest to the correct script, but I had to add $spSourceListItesm = spSourceList.Items so that I could reference the ID of each item in the list via $item.ID which is the last line of the script. Otherwise, $item.ID was blank. Hopefully you found this blog post first and didn’t have to dig through a lot of non-working sources.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s