How to start SharePoint list workflow using PowerShell automatically?

Instantly start workflow in SharePoint: How to start SharePoint list workflow using PowerShell in just 2 step

No comments

Loading

In this “start workflow in SharePoint” blog, we will learn how to start the SharePoint list workflow using PowerShell script. Many times, due to business reasons or technical problems we might need to start the SharePoint list workflow programmatically for all the list items.

Key Highlights: Start workflow in SharePoint using PowerShell

  • How to start the SharePoint list workflow using PowerShell.

PowerShell script to start the SharePoint list workflow programmatically for all the list items.

Using the below PowerShell script we can start workflow in SharePoint:

start workflow in SharePoint using PowerShell automatically
How to start the SharePoint list workflow using PowerShell automatically?

#The below script is used to start the workflow in SharePoint list items programmatically.

Add-PSSnapin Microsoft.Sharepoint.Powershell
cls

$fileName = "Workflow Start Report"
#'yyyyMMddhhmm yyyyMMdd
$enddate = (Get-Date).tostring("yyyyMMddhhmmss")
#$filename = $enddate + '_VMReport.doc'
$logFileName = $fileName +"_"+ $enddate+"_Log.txt"
$invocation = (Get-Variable MyInvocation).Value
$directoryPath = Split-Path $invocation.MyCommand.Path

$directoryPathForLog=$directoryPath+"\"+"LogFiles"
if(!(Test-Path -path $directoryPathForLog))
{
New-Item -ItemType directory -Path $directoryPathForLog
#Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
#$logPath = $directoryPath + "\" + $logFileName

$logPath = $directoryPathForLog + "\" + $logFileName

$isLogFileCreated = $False

function Write-Log([string]$logMsg)
{
if(!$isLogFileCreated){
Write-Host "Creating Log File..."
if(!(Test-Path -path $directoryPath))
{
Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
}
else
{
$script:isLogFileCreated = $True
Write-Host "Log File ($logFileName) Created..."
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}
else
{
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
}
}

$web = Get-SPWeb "http://server:port"; #Your site name
$wfManager = $web.Site.WorkFlowManager;
$myList = $web.Lists["TestList"]; #Your list name

$association = $myList.WorkflowAssociations.GetAssociationByName("Test Workflow 2020","en-US"); #Pass your list name and culture string like en-US

$myView = $myList.Views["All Items"]; #Pass the view name like "All Items"

$listItems = $myList.GetItems($myView);
$associationData = $association.AssociationData;

foreach ($oneItem in $listItems)
{
try
{
$wf = $wfManager.StartWorkFlow($oneItem,$association,$associationData,$true);

$message="Workflow has been started on: "+$oneItem
Write-Host $message -BackgroundColor Green
}
catch
{
$ErrorMessage = $_.Exception.Message +"in starting the workflow on!: " +$oneItem
Write-Host $ErrorMessage -BackgroundColor Red
Write-Log $ErrorMessage

}
}

Write-host "The execution has been completed Successfully "

Summary: Start workflow in SharePoint using PowerShell

Thus, in this PowerShell script – we have learned about how to start a SharePoint workflow for all items in the list using the PowerShell script programmatically or how to start a workflow on multiple items on a list.

  • Learned about how to start the SharePoint list workflow using PowerShell.

See Also: SharePoint PowerShell tutorial

You may also like the following SharePoint PowerShell tutorials:

 

About Post Author

Do you have a better solution or question on this topic? Please leave a comment