Power Automate allows users to track the run history of their flows to analyze performance, debug issues, and ensure the flow runs as expected. Using PowerShell, you can easily retrieve and analyze the run history for your Power Automate flows. This guide will walk you through how to check the run history of your flows using PowerShell.
Step 1: Install Required PowerShell Modules
Before you can interact with Power Automate, make sure to install the necessary PowerShell modules for administration:
Install-Module -Name Microsoft.PowerApps.Administration.PowerShell -Force -AllowClobber
Install-Module -Name Microsoft.PowerApps.PowerShell -Force -AllowClobber
If prompted, press Y to confirm the installation.
Step 2: Authenticate to Power Platform
To access your Power Automate data, you need to authenticate:
Add-PowerAppsAccount
Alternatively, for Microsoft Graph API, you can authenticate with:
Connect-MgGraph -Scopes "User.Read.All", "Application.Read.All"
Step 3: Identify the Target Environment
The next step is to identify which environment the flow belongs to. List all environments to find the Environment ID:
Get-AdminEnvironment | Select-Object DisplayName, EnvironmentName | Format-Table -AutoSize
🔹 Note the Environment Name where the flow is located.
Step 4: List All Flows in the Environment
To retrieve run history, you first need to get the list of flows in your environment. You can use the following command:
$EnvironmentName = "<YourEnvironmentID>" # Replace with your environment ID
Get-AdminFlow -EnvironmentName $EnvironmentName | Select-Object DisplayName, FlowName, State | Format-Table -AutoSize
🔹 This lists all the flows in the specified environment. Find the FlowName you want to check the run history for.
Step 5: Retrieve the Run History for a Flow
To get the run history for a specific flow, use the FlowName and run the following command:
$FlowName = "<YourFlowName>" # Replace with your flow's name
$EnvironmentName = "<YourEnvironmentID>" # Replace with your environment ID
Get-AdminFlowRunHistory -EnvironmentName $EnvironmentName -FlowName $FlowName | Select-Object RunName, Status, StartTime, EndTime, Duration, ErrorDetails | Format-Table -AutoSize
🔹 The command will return the run history of the flow, including:
- RunName: The name of the run.
- Status: Whether the run was Succeeded, Failed, or Cancelled.
- StartTime: When the flow run started.
- EndTime: When the flow run ended.
- Duration: How long the flow run took.
- ErrorDetails: Information about any errors encountered during the run.
Step 6: Filter Run History for Specific Criteria
If you want to narrow down the results, you can filter based on specific criteria like status or date:
Example 1: Get only Failed Runs:
Get-AdminFlowRunHistory -EnvironmentName $EnvironmentName -FlowName $FlowName | Where-Object { $_.Status -eq "Failed" } | Select-Object RunName, Status, StartTime, ErrorDetails | Format-Table -AutoSize
Example 2: Get Runs for the Last Week:
$LastWeek = (Get-Date).AddDays(-7)
Get-AdminFlowRunHistory -EnvironmentName $EnvironmentName -FlowName $FlowName | Where-Object { $_.StartTime -gt $LastWeek } | Select-Object RunName, Status, StartTime, EndTime | Format-Table -AutoSize
Step 7: Export Run History to CSV
If you want to export the run history for reporting or further analysis, use the following command to export it to a CSV file:
$RunHistory = Get-AdminFlowRunHistory -EnvironmentName $EnvironmentName -FlowName $FlowName
$RunHistory | Select-Object RunName, Status, StartTime, EndTime, Duration, ErrorDetails | Export-Csv -Path "C:\Path\To\Export\FlowRunHistory.csv" -NoTypeInformation
Write-Host "Run history has been exported to 'FlowRunHistory.csv'."
🔹 This will save the run history into a CSV file for easier analysis.
Step 8: Get Detailed Information for a Specific Run
If you need more detailed information about a specific run, such as the actions executed during the flow’s run, you can retrieve the details of a specific run by using the RunID:
$RunID = "<RunID>" # Replace with the actual Run ID you want details for
$EnvironmentName = "<YourEnvironmentID>"
$FlowName = "<YourFlowName>"
Get-AdminFlowRunHistoryDetail -EnvironmentName $EnvironmentName -FlowName $FlowName -RunID $RunID
This will give you detailed information about each action and the status of the run.
Step 9: Automating Run History Monitoring
If you need to regularly monitor flow runs, you can automate the process by saving the script to a .ps1
file and scheduling it to run at regular intervals. Here’s an example of how you can automate this task:
$ScriptPath = "C:\PowerAutomate\MonitorFlowRunHistory.ps1"
$Trigger = New-ScheduledTaskTrigger -Daily -At 3am
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File $ScriptPath"
Register-ScheduledTask -TaskName "DailyFlowRunHistory" -Trigger $Trigger -Action $Action -RunLevel Highest -User "NT AUTHORITY\SYSTEM"
🔹 This will run the PowerShell script daily at 3 AM, and you can use this to log flow statuses or send notifications.
Step 10: Disconnect PowerShell Session
Once you’ve completed your tasks, disconnect from the session:
Disconnect-MgGraph
Disconnect-PowerAppsAccount