SharePoint Online: Start Workflow on All Items in a List using PowerShell

Requirement: Run Workflow in SharePoint Online using PowerShell

Start Workflow on All Items in a SharePoint Online List using PowerShell

SharePoint Online: PowerShell to Start Workflow

This PowerShell script starts a given workflow on all items in a SharePoint Online list:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"

#Set Parameters
$ListName ="Project Tasks"
$WorkflowName ="Send Approval Email"

#Get Credentials to connect
$Cred= Get-Credential

    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
    #Get the Web, List and List Items Object
    $Web = $Ctx.Web
    $List = $Web.Lists.GetByTitle($ListName)
    $ListItems = $List.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())

    #Initialize Workflow Manager and other related objects
    $WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($Ctx, $Web)
    $WorkflowSubscriptionService = $workflowServicesManager.GetWorkflowSubscriptionService()
    $WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService() 
    $WorkflowAssociations = $WorkflowSubscriptionService.EnumerateSubscriptionsByList($List.Id)

    #Get the Target workflow to Run
    $WorkflowAssociation = $WorkflowAssociations | Where {$_.Name -eq $WorkflowName}
    #Start Workflow on each List Item
    ForEach($ListItem in $ListItems)
        #sharepoint online powershell start workflow        
        $Dict = New-Object 'System.Collections.Generic.Dictionary[System.String,System.Object]'
        $Action = $WorkflowInstanceService.StartWorkflowOnListItem($WorkflowAssociation, $ListItem.Id, $Dict)
Catch {
Write-host -f Red "Error:" $_.Exception.Message

This PowerShell runs workflow in each item of a list in SharePoint Online.

PnP PowerShell to Start Workflow in SharePoint Online

This time, let’s run a specific SharePoint Online workflow using PowerShell on a particular item. Here is how to trigger SharePoint Online workflow using PnP PowerShell:

$SiteUrl = ""
$ListName = "IC Docs"
$WorkflowName = "Set Document Permissions"
$ItemID = 23

#Connect to SharePoint Online 
Connect-PnPOnline $SiteUrl -Interactive
#Get the Workflow to Run
$WFSubscription = Get-PnPWorkflowSubscription -List $ListName -Name $WorkflowName

#Start Workflow 
Start-PnPWorkflowInstance -Subscription $WFSubscription -ListItem $ItemID

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

One thought on “SharePoint Online: Start Workflow on All Items in a List using PowerShell

  • Hello Salaudeen,

    If there is a PnP to start a workflow, How about to end it?


Leave a Reply

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