Find All Workflows In SharePoint Using PowerShell

Workflows are a quintessential example of automating manual tasks in SharePoint. Besides automations, they function as the heart of many organizations’ IT landscape by providing solutions such as approval mechanisms, document management, user onboarding, etc. The uses and benefits are many.
 
But looking through the lens of a Farm Admin or Site Collection Admin, there does not exist any report or out of the box means to provide a detailed list of workflows and their usage, dependent lists etc.
 
ShareGate has recently rolled out an update using which you can use to generate a very comprehensive report for Workflow usage in a site collection. And for others (like me!) who use PowerShell almost everywhere and tackle almost every scenario, welcome to my article!
This script will identify and find out the list of workflows that are published within a site collection. The idea is to iterate through all the child webs and within each web will investigate all the lists that are using workflows. This script can report all the OOTB workflows as well as SPD workflows. Please note that, this script cannot identify site workflows.
 
Script
  1. if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)  
  2. {  
  3.   
  4.     Add-PSSnapin "Microsoft.SharePoint.PowerShell"  
  5. }  
  6.   
  7. $results = @()  
  8. $siteColl =  "http://sharepoint.starkindustries.com/it"  
  9.    
  10. $site = Get-SPSite -Identity $siteColl -Limit All  
  11. try  
  12. {   
  13.     foreach ($myWeb in $site.AllWebs)  
  14.     {  
  15.         Write-Host "Looking in Web: " $myWeb.Url -ForegroundColor Red  
  16.         foreach($list in $myWeb.Lists)  
  17.         {  
  18.             if ($list.WorkflowAssociations -eq $true)  
  19.             {  
  20.                 Write-Host $list.Title -ForegroundColor Blue  
  21.                 foreach ($wflowAssociation in $list.WorkflowAssociations)  
  22.                 {  
  23.                     $RowDetails =  @{            
  24.                       "List Name"         = $wflowAssociation.ParentList.Title  
  25.                       "Workflow Name"     = $wflowAssociation.InternalName  
  26.                       "Running Instances" = $wflowAssociation.RunningInstances  
  27.                       "Created On"        = $wflowAssociation.Created  
  28.                       "Modified On"       = $wflowAssociation.Modified  
  29.                       "Parent Web"        = $wflowAssociation.ParentWeb  
  30.                       "Task List"         = $wflowAssociation.TaskListTitle  
  31.                       "History List"      = $wflowAssociation.HistoryListTitle                   
  32.                     }  
  33.   
  34.                     $results += New-Object PSObject -Property $RowDetails  
  35.                 }            
  36.             }  
  37.   
  38.         }  
  39.     }  
  40.   
  41.     $myFileName = [Environment]::GetFolderPath("Desktop") + "\workflowList.csv"  
  42.     $results | Select-Object "List Name""Workflow Name""Running Instances""Created On","Modified On","Parent Web""Task List","History List"    | export-csv -Path $myFileName -NoTypeInformation  
  43.   
  44. }  
  45.   
  46. catch   
  47. {   
  48.     $e = $_.Exception   
  49.     $line = $_.InvocationInfo.ScriptLineNumber   
  50.     $msg = $e.Message   
  51.     Write-Host –ForegroundColor Red "Caught Exception: $e at $line"   
  52.     Write-Host $msg   
  53.     Write-Host "Something went wrong"  
  54. }   
  55.   
  56. Write-Host " === === === === === Completed! === === === === === === == "  
Illustration
 
The code iterates through all the child webs within a site collection. It then flags the on the WorkflowAssociations property of the list, as shown in the line below,
  1. if ($list.WorkflowAssociations -eq $true)  
Once it gets inside that loop, it then collects all the appropriate list identifiers as well as the necessary workflow details. It also captures the task list and the history list of the workflows, along with the count of running instances.
 
Usage
  • In the script, just change the site collection URL and run it.
  • The output CSV file will be generated in the desktop with the name workflowList.csv 
The code can also be found in my GitHub repo.