Start List Item Workflows Using PowerShell

By James|October 24, 2020|, ,

Have you ever needed to run a workflow against all items in a SharePoint list?  If so, you know that using the web interface to do this is a slow tedious process.  You can use the following script to programmatically run a workflow against every item in a list:

# Title:   DailyWorkflows.ps1
# Version: 1.0, 18 AUG 2020
# Author:  James Sanders
# Purpose: Daily portal workflows

# Add the PowerShell Snap-In
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

# Configure environment
$webURL   = "http://portal/site"
$listName = "List_Name"
$wfName   = "Workflow_Name"

# Open Web Site
Write-Host "`n- Opening web site $webURL"
$web = Get-SPWeb $webURL -ErrorAction SilentlyContinue

if (!($web)) {
  Write-Host -ForegroundColor Red "`nUnable to open web site $webURL"
  Exit
}

# Open List
Write-Host "- Opening list $listName"
$list = $web.lists[$listName]

if (!($list)) {
  Write-Host -ForegroundColor Red "`nUnable to open list $listName"
  Exit
}

# Get site workflow manager
$manager = $web.Site.WorkflowManager

# Get workflow association
$assoc = $list.WorkflowAssociations.GetAssociationByName($wfName, "en-US")

if (!($assoc)) {
  Write-Host -ForegroundColor Red "`nUnable to access workflow $wfName"
  Exit
}

$data  = $assoc.AssociationData

# Process List
$itemCurrent = 1
$itemTotal = $List.Items.Count

ForEach ($item in $List.Items) {
  $itemCurrentF = $itemCurrent.ToString().PadLeft($itemTotal.ToString().Length,"0")
  Write-Host "- Processing list item $itemCurrentF of $($itemTotal)"
  $wf = $manager.StartWorkFlow($item, $assoc, $data, $true)
  $itemCurrent++
}

# Clean up
$manager.Dispose()
$web.Dispose()

 

Copyright 2011 - 2021 The Lazy SharePoint Admin | All Rights Reserved
menu-circlecross-circle linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram