Start a workflow using PowerShell

Requirement

Start a workflow on all / specific items in a list.

Change the site and workflow names to suit your requirements. I included a condition in this script to only start the workflow on items where ‘Status’ is not equal to Complete. You can remove this condition if you wish to run the script on all items in the list.

Solution

Use a PowerShell script to start the workflow.

#Site and List  
$SPAssignment = Start-SPAssignment 
$web = Get-SPWeb "http://your-site" -AssignmentCollection $spAssignment 
$site = Get-SPSite "http://webservices.rcrtom.com.au"
$listToCancel = "your-list";
$web.AllowUnsafeUpdates = $true;
 
#Output file
$Today = Get-Date -Format "dd-M-yy"
$outFile = "c:\CancelWorkflowScript-Log-$Today.txt"
 
#Workflows to Remove
$wfToStart= "Name of workflow"
 
#List Name  
$list = $web.Lists[$listToCancel]; 
 
#Workflow Manager
$manager=$site.WorkFlowManager
$association=$list.WorkFlowAssociations | where {$_.Name -eq $wfToStart}
$association.AllowAsyncManualStart = $true
$association.AllowManual = $true
 
# Iterate through all Items in List and all Workflows on Items.  
	#Filtered List
	Write-Host "Restarting workflows.."
	foreach ($item in $list.Items) { 
		if ($item["Status"] -ne "Completed") {
			$data=$association.AssociationData
			$wf=$manager.StartWorkFlow($item,$association,$data)
			Write-Output "$wftoStart started on " $item.Name | Out-File $outFile -Append
		}
		else {
			Write-Output $item.Name " - is completed " | Out-File $outFile -Append
 
		}
	}
Write-Host Finished.
$web.Dispose() 
$site.Dispose()

Note: Workflows started using via PowerShell like this do not trigger other workflows when they complete, even if you have other workflows set to automatically start on item change.

Tags: ,

2 Responses to “Start a workflow using PowerShell”

  1. Sergey June 30, 2015 at 4:36 pm #

    Why use $site = Get-SPSite “http://webservices.rcrtom.com.au”?

    • Erik November 6, 2015 at 5:10 pm #

      Because the WorkflowManager is a member of the site object, not web.

      $manager=$site.WorkFlowManager

Leave a Reply

%d bloggers like this: