To Execute the Workflow Instance through Power-Shell Script

To Start SPD/Nintex Workflow through PowerShell Script.

In this article, we are going to see how to run the SharePoint Designer workflow /Nintex workflow through PowerShell script for bulk list items.

Through this PowerShell script we can run the workflow to multiple list items (if the workflow associated both item created and item updated events).

PowerShell Script:

if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )

{

Add-PsSnapin Microsoft.SharePoint.PowerShell

}

#Provide Site URL and List Name

$siteUrl = 'Provide Site URL'

$listurl= 'Provide List URL'

$site = new-object Microsoft.SharePoint.SPSite($siteUrl)

$web = $site.OpenWeb();

$manager = $web.Site.WorkFlowManager

# Name of the list

write-host $listurl

$list = $web.getList($listurl)

$wfrunoption=[microsoft.sharepoint.workflow.spworkflowrunoptions]::synchronous

# Name of the Workflow

$assoc = $list.WorkflowAssociations.GetAssociationByName("Provide Workflow Name","en-US")

write-Host $assoc.Name

$data = $assoc.AssociationData

$items = $list.Items

foreach($item in $items)

{

if($itemNo -Ne $itemCount)

{

$wf = $manager.StartWorkFlow($list.GetItemById($item.ID),$assoc,$data,$true)

}

}

$manager.Dispose()

Write-Host "Workflow Completed Successfully"

$web.Dispose()

StartWorkflow Method Parameters List:

$list.GetItemByID($item.ID) à Starts a workflow instance for specific list item.

$association à The workflow association on which to base the new

Workflow Instance.

$data (event Data) à A string that contains event parameters.

RunOption àIf the argument is RunOptions.Synchronous, the method tries to start the workflow immediately; if it cannot be started immediately, then the method returns a null reference (Nothing in Visual Basic).

If the argument is RunOptions.SynchronousAllowPostpone, the method tries to start the workflow immediately; if it cannot be started immediately, it is deferred and the method returns an SPWorkflow object referencing the unstarted workflow.

If the argument is RunOptions.Asynchronous, then the start of the workflow is deferred and the method returns an SPWorkflow object referencing the unstarted workflow.

Conclusion

In this article, I explained how to do an execute the workflow instance through workflow start method.