PowerShell - Get All Workflow In SharePoint Online

In this blog, I will try to fetch all workflows which are created inside different Site collection in SharePoint Online.

Introduction
 
Many times we have a requirement to list out all workflows which have been created in SharePoint Online in order to rewrite/scrap useless workflows or because of some other reason.
 
You can modify this workflow based on your requirement if you want to have more details about the list/site/item.
 
The current workflow performs the below actions,
  1. Gets all workflows from all lists and libraries from SharePoint Online site collection.
  2. Exports the workflow data into a CSV file
References
 
You need to add the below 3 references in your ps1 file,
 
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"
 
Later you need to update only with your site collection URL at:
 
$SiteURL="Your SharePoint Online Site Collection URL"
 
Complete Script
  1. #Start Code  
  2.  
  3. #Load SharePoint CSOM Assemblies  
  4. Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
  5. Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"  
  6. Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.WorkflowServices.dll"  
  7.  
  8. #Function to Get workflows in a site  
  9.  
  10. #Set Parameters  
  11. $SiteURL="Your Site URL"  
  12. $CSVPath = "D:\Project\WorkflowInventoryGCP.csv"  
  13.    
  14.    
  15. Function GetNintexForm(){  
  16.    $SiteUrl = "Site URL"  
  17.    $UserName = "User Name"  
  18.    #Bind to site collection  
  19.    $SecurePassword = ConvertTo-SecureString "Password" -AsPlainText -Force  
  20.    $ClientContext = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)  
  21.    $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)  
  22.    $ClientContext.Credentials = $credentials  
  23.    $ClientContext.ExecuteQuery()  
  24.    # Create WorkflowServicesManager instance  
  25.    $WorkflowServicesManager = New-Object Microsoft.SharePoint.Client.WorkflowServices.WorkflowServicesManager($ClientContext, $ClientContext.Web)  
  26.    # Connect to WorkflowSubscriptionService  
  27.    $WorkflowSubscriptionService = $WorkflowServicesManager.GetWorkflowSubscriptionService()  
  28.    # Connect WorkflowInstanceService instance  
  29.    $WorkflowInstanceService = $WorkflowServicesManager.GetWorkflowInstanceService()  
  30.    $ClientContext.Load($WorkflowServicesManager)  
  31.    $ClientContext.Load($WorkflowSubscriptionService)  
  32.    $ClientContext.Load($WorkflowInstanceService)  
  33.    $ClientContext.ExecuteQuery()  
  34.    # Get Site Workflows  
  35.    $SiteWorkflows = $WorkflowSubscriptionService.EnumerateSubscriptions()  
  36.    $ClientContext.Load($SiteWorkflows)  
  37.    $ClientContext.ExecuteQuery()  
  38.    $SiteWorkflows | Select Id, Name, StatusFieldName | FT -AutoSize  
  39.   
  40.    If($SiteWorkflows) { $SiteWorkflows | Export-CSV -LiteralPath $CSVformPath -NoTypeInformation -Append}  
  41.    # Define Site Workflow  
  42.    # $SiteWorkflowID = 'mysiteworkflowid'  
  43.    # $SiteWorkflow = $WorkflowSubscriptionService.GetSubscription($SiteWorkflowID)  
  44.    # $ClientContext.Load($SiteWorkflow)  
  45.    # $ClientContext.ExecuteQuery()  
  46.    # Prepare Start Workflow Payload  
  47.    $Dict = New-Object 'System.Collections.Generic.Dictionary[System.String,System.Object]'  
  48.    # Loop Start Workflow  
  49.    For ($j=0; $j -lt 10; $j++){  
  50.       $Action = $WorkflowInstanceService.StartWorkflow($SiteWorkflow, $Dict)  
  51.       #$ClientContext.ExecuteQuery()  
  52.       # Get Workflow Status  
  53.       #$WorkflowInstance = $Action.Value  
  54.       #$WorkflowStatus = $WorkflowInstanceService.GetInstance($Action.Value)  
  55.       #$ClientContext.Load($WorkflowStatus)  
  56.       #$ClientContext.ExecuteQuery()  
  57.       #Write-Host $Action.Value $WorkflowStatus.Status  
  58.       }  
  59.    }  
  60. #End  
  61.  
  62. #Remove the CSV file if exists  
  63. If(Test-Path $CSVPath) { Remove-Item $CSVPath}  
  64.  
  65. #Get Credentials to connect  
  66. $Cred= Get-Credential  
  67.  
  68. #Call the function to get workflow inventory  
  69. GetNintexForm  
  70. #End Code  
The output of the script will be:
 
 
 

Also, I have uploaded a ps file along with this, just change the URL to yours and start using it.
 
Happing Coding!!