Thursday, May 10, 2012

PowerShell Script: Identify All Workflows in Your SharePoint Farm


The following script will return all Workflows currently associated within your SharePoint 2007 or SharePoint 2010 farm using PowerShell and output it to a CSV file.  It returns the URL of the site, Title of the list, Title of the workflow, and the number of currently running instances.

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null 
$outputFile = Read-Host "Filename and location (i.e. C:\output.txt)"

$farm = [Microsoft.SharePoint.Administration.SPFarm]::local
$websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]} 
$webapps = @() 

$outputHeader = "Url;List;Workflow;Running Instances" > $outputFile

foreach ($websvc in $websvcs) { 

    foreach ($webapp in $websvc.WebApplications) { 
  foreach ($site in $webapp.Sites) {
   foreach ($web in $site.AllWebs) {
    foreach ($List in $web.Lists) {
     foreach ($workflow in $List.WorkflowAssociations) {
      $output = $web.Url + ";" + $List.Title + ";" + $workflow.Name + ";" + $workflow.RunningInstances
      Write-Output $output >> $outputFile
    }}
    } 
   } 
  }
 }
$Web.Dispose();
$site.Dispose();

It can also be downloaded from my Google Drive here: CheckWorkflowsInFarm.zip 

Output Example:

Url;List;Workflow;Running Instances
http://intranet;Report a Problem or New Request;Notification Workflow for Bug List (Previous Version:4/13/2011 3:01:15 PM);0
http://intranet;Report a Problem or New Request;Notification Workflow for Bug List;0
http://intranet;Report a Problem or New Request;Notification Workflow for Bug List (Previous Version:4/13/2011 3:04:13 PM);0
http://intranet/contracts;PDF Doc Library;Send to Sales;0
http://intranet/contracts;PDF Doc Library;Send to Sales (Previous Version:6/4/2010 5:33:55 PM);0
http://intranet/contracts;Doc Library;DOC Approval (Previous Version:6/29/2010 3:55:22 PM);1
http://intranet/contracts;Doc Library;DOC Approval;0
http://intranet/contracts;Doc Library;DOC Approval (Previous Version:6/29/2010 3:49:51 PM);0
http://intranet/contracts;Doc Library;DOC Approval (Previous Version:6/29/2010 3:37:48 PM);0
http://intranet/contracts;Doc Library;DOC Approval (Previous Version:6/29/2010 11:18:26 AM);0