Powershell to start workflow on SharePoint list

Recently, I had to write the powershell script to start the workflow on SharePoint list items. This was proposed a workaround for the problem when items added by the anonymous users was not triggering the workflow sometimes. I proposed to run this powershell to start workflow items on which workflow is not started and scheduled this to run daily using windows task scheduler.

In this post I have provided the powershell script shown below (replace the siteurl, listname and workflow name) and then explained how to run it through task scheduler.

Powershell script

$SPAssignment = Start-SPAssignment
$web = Get-SPWeb “http://serverurl/sites/somesite” -AssignmentCollection $spAssignment
$site = Get-SPSite “http://serverurl
$listname = “SomeList”;
$web.AllowUnsafeUpdates = $true;

$Today = Get-Date -Format “dd-M-yy”
$outFile = “c:\manvir\CancelWorkflowScript-Log-$Today.txt”

#Workflows to Remove
$wfToStart= “someworkflow”

#List Name
$list = $web.Lists[$listname];

#Workflow Manager
$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”) {
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.


Steps to schedule powershell script to run using Windows task scheduler

1) Save the above file as ‘TriggerWorkflow.ps1″.

2) Now, we need to create a batch file that will run this powershell script. Please note we dont want to directly run this powershell file using task scheduler because doing so doesnt start the workflow. Thus, we need to first create a batch file that will run this powershell file. You batch file should look like this –

powershell .\TriggerWorkflow.ps1
3) Save the batch file in the same folder where ‘TriggerWorkflow.ps1’ is saved and name it as ‘TriggerWorkflow.bat’. Let’s say the location is “c:\Manvir”.

4) Create a new task in task scheduler that should look something like this as shown below –


Leave a Comment

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