Run Workflow For All Items Of List In SharePoint

In some scenarios, we might need to develop workflows for the existing SharePoint list which has several list items. This happens because of some enhancements or user(s) requirements. Normally, we develop a workflow for lists or libraries, and workflow can execute manually, item add or update automatically.
 
Note
workflow means SharePoint workflow which is developed via SharePoint designer.
 
However, in some cases, we need to run workflow all items in List which is very panicked work via SharePoint GUI. We must select each list item and run workflow manually which is not effective or feasible in case there are  too many items in a list. Additionally, it is a time-consuming task to run a workflow for each item.
 
Here, I am sharing a small piece of code in PowerShell command through which we can accomplish this requirement but note this will not start those workflows instantly. However, it can take up-to 5 minutes as it is triggered through SharePoint Timer Service. (use SharePoint PowerShell)
  1. $webApp = Get-SPWebApplication "http://sharepointUrl/sites/sitename"    
  2. # URL of the Site    
  3. $web = Get-SPWeb -Identity "http://sharepointUrl/sites/sitename"    
  4. $workflowmanager = $web.Site.WorkFlowManager    
  5. Name of the list    
  6. $list = $web.Lists["List Name"]    
  7. Name of the Workflow    
  8. $assocname = $list.WorkflowAssociations.GetAssociationByName("On Item Created","en-US")    
  9. $data = $assocname.AssociationData    
  10. $items = $list.Items    
  11. foreach($item in $items)    
  12.  {    
  13.     $workflow = $workflowmanager.StartWorkFlow($item,$assocname,$data,$true)    
  14.  }    
  15. $workflowmanager.Dispose()    
  16. $web.Dispose()    
After running this PowerShell script, the workflow will be triggered for each item in the mentioned list. This small Powershell script saves huge time compared to when we manually to run the workflows.