You could try below script.
#Load SharePoint CSOM Assemblies
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"
#Function to Get workflows in a site
Function Get-SPOWorkflowInventory($SiteURL, $CSVPath)
{
Try{
$WorkflowInventory = @()
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
#Get the Web and its Subsites
$Web = $Ctx.Web
$Ctx.Load($Web)
$Ctx.Load($Web.Webs)
$Lists = $Web.Lists
$Ctx.Load($Lists)
$Ctx.ExecuteQuery()
Write-host -f Yellow "Searching Workflows in Site: $SiteURL"
#Loop through each list and get all workflows sharepoint online powershell
ForEach($List in $Lists)
{
#Get SharePoint 2010 Workflows Associated
$WorkflowAssociations = $List.WorkflowAssociations
$Ctx.Load($WorkflowAssociations)
$Ctx.ExecuteQuery()
ForEach($Association in $WorkflowAssociations | Where {$_.Name -notlike "*Previous Version*"})
{
$WorkflowData = New-Object PSObject
$WorkflowData | Add-Member NoteProperty WorkflowName($Association.Name)
$WorkflowData | Add-Member NoteProperty SiteURL($Web.Url)
$WorkflowData | Add-Member NoteProperty ListName($List.Title)
Write-host -f Green "`t Found Workflow '$($Association.Name)' in list '$($List.Title)'"
$WorkflowInventory+=$WorkflowData
}
}
#Export Workflow data to CSV File
If($WorkflowInventory) { $WorkflowInventory | Export-CSV -LiteralPath $CSVPath -NoTypeInformation -Append}
#Process Subsites
Foreach($Subweb in $Web.Webs)
{
Get-SPOWorkflowInventory -SiteURL $Subweb.url
}
}
Catch {
Write-host -f Red "Error:" $_.Exception.Message
}
}
#Set Parameters
$SiteURL="site collection URL "
$CSVPath = "your local drive"
#Remove the CSV file if exists
If(Test-Path $CSVPath) { Remove-Item $CSVPath}
#Get Credentials to connect
$Cred= Get-Credential
#Call the function to get workflow inventory
Get-SPOWorkflowInventory $SiteURL $CSVPath
Here're some references for you.
Get SharePoint Online workflows by using PowerShell CSOM
SharePoint Online get workflow inventory using PowerShell
If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.