How to cancel all your running Power Automate Flow runs Using M365 CLI PowerShell

Power Automate Flow Runs: Cancel All Your Running Flows

No comments


In this “Power Automate Flow Runs” article, we will learn how to cancel all instances of a running Power Automate flow from a given environment using PowerShell and PnP PowerShell. We will also learn what Microsoft 365 CLI is and how to install M365 CLI packages on your laptop. In my previous article, I showed how to cancel all running instances of Power Automate flow using Power Automate.

Microsoft Power Automate, formerly known as Microsoft Flow, is a powerful tool that enables organizations to automate repetitive tasks and workflows. While creating and managing flows is straightforward, there may be times when you need to cancel running flow runs to maintain control over your processes or address issues as they arise. In this article, we’ll explore how to cancel all your running Power Automate flow runs using the M365 CLI PowerShell.

Cancel All Your Running Power Automate Flow: Why do we need to Cancel Flow Runs?

There are several reasons why you might want to cancel running flow runs in Power Automate:

  • Error Handling: If a flow run encounters an error and you want to prevent it from causing further issues, you may need to cancel it.
  • Testing and Debugging: During the development and testing phase of your flows, you might want to halt the execution of specific runs to investigate issues and make improvements.
  • Resource Management: Canceling unnecessary flow runs can help you optimize your Power Automate resources and ensure that critical processes are not slowed down.
  • Compliance and Data Management: In scenarios where compliance or data management requirements demand that certain flow runs be terminated, you’ll need the capability to cancel them.

What is CLI for Microsoft 365?

Irrespective of your platform of choice—Windows, macOS, or Linux—and your preferred command-line interface, be it Bash, Cmder, or PowerShell, the CLI for Microsoft 365 allows you to efficiently administer your Microsoft 365 tenant and handle SharePoint Framework projects. You can fine-tune Microsoft 365 configurations, oversee SharePoint Framework endeavors, and create automation scripts with ease.


The CLI for Microsoft 365 is distributed as an NPM package. To use it, install it globally using:

npm i -g @pnp/cli-microsoft365

or using yarn:

yarn global add @pnp/cli-microsoft365

Canceling Flow Runs with M365 CLI PowerShell

The M365 CLI is a versatile command-line tool that allows you to manage various Microsoft 365 services, including Power Automate. To cancel running flow runs, you’ll need to use the M365 CLI PowerShell module.

Here are the steps to cancel all your running Power Automate flow runs using M365 CLI PowerShell:

Login to Microsoft 365

Once the CLI is installed successfully, the next step is to login. To login use the below command. You’ll be prompted to sign in using your Microsoft 365 credentials and use the browser mode to login using the code and credentials.

m365 login

Microsoft 365 CLI Login

Microsoft 365 CLI Login

What can we perform using CLI commands?

Once your login is completed, we can perform multiple operations on Microsoft 365 services.

After authentication, you can start using the CLI for Microsoft 365 by running various commands to manage different aspects of your Microsoft 365 environment. For example, you can create and manage SharePoint sites, configure Exchange Online settings, and interact with Azure Active Directory.

Here are some common tasks you can perform with the CLI for Microsoft 365:

  • Manage SharePoint sites, lists, and libraries.
  • Create and manage Microsoft Teams.
  • Work with Azure Active Directory users and groups.
  • Configure and manage Exchange Online settings.
  • Export and import data from Microsoft 365 services.
  • Automate administrative tasks in your Microsoft 365 tenant.

To see a list of available commands and their descriptions, you can use the following command:

m365 help

You can also get help on specific commands by running:

m365 <command></command> --help

For example, to get help on SharePoint commands, you can use:

m365 spo --help

The CLI for Microsoft 365 is a versatile tool that can save you time and streamline your administrative tasks within the Microsoft 365 ecosystem. It allows you to script and automate complex operations, making it a valuable resource for Microsoft 365 administrators and developers.

Script: Cancel All Your Running Power Automate Flow Runs Using M365 CLI PowerShell

By now, we have completed the prerequisites and setup for the running cancel flow runs PowerShell script. Now let’s move on to the agenda of this article: how to cancel all your running Power Automate flow runs using Microsoft 365 CLI PowerShell.

PowerShell script: Cancel All Your Running Power Automate Flow Runs

After your successful login to Microsoft 365, execute the below PowerShell script:

####The below script is used to cancel all flow runs from the Power Platform given environment. #############
$PSshell = Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorVariable err -ErrorAction SilentlyContinue
if($PSshell -eq $null)
Add-PSSnapin "Microsoft.SharePoint.PowerShell"

$fileName = "CancelAllFlowRunsLog"

#'yyyyMMddhhmm yyyyMMdd

$enddate = (Get-Date).tostring("yyyyMMddhhmmss")

#$filename = $enddate + '_VMReport.doc'

$logFileName = $fileName +"_"+ $enddate+"_Log.txt"

$invocation = (Get-Variable MyInvocation).Value

$directoryPath = Split-Path $invocation.MyCommand.Path


if(!(Test-Path -path $directoryPathForLog))
New-Item -ItemType directory -Path $directoryPathForLog
#Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
#$logPath = $directoryPath + "\" + $logFileName
$logPath = $directoryPathForLog + "\" + $logFileName
$isLogFileCreated = $False
function Write-Log([string]$logMsg)

Write-Host "Creating Log File..."
if(!(Test-Path -path $directoryPath))
Write-Host "Please Provide Proper Log Path" -ForegroundColor Red
$script:isLogFileCreated = $True
Write-Host "Log File ($logFileName) Created..."
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage
[string]$logMessage = [System.String]::Format("[$(Get-Date)] - {0}", $logMsg)
Add-Content -Path $logPath -Value $logMessage

#This is to t load SharePoint CSOM Assemblies - you should have the below dlls in the given location.

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"

#Configuration Parameters

$yourPowerPlatformEnvirontmentGUID="It should be your Power Platform Environment GUID value"

$yourflowGUID="It should be your flow GUID value"

#Configuration Parameters - Ends here


m365 login
$yourCurrentFlowRuns = m365 flow run list --environmentName $yourPowerPlatformEnvirontmentGUID --flowName $yourflowGUID --output json | ConvertFrom-Json
foreach ($oneRun in $yourCurrentFlowRuns) 
if($oneRun.status -eq "Running")
Write-Output "Flow run details: " $oneRun
#The below line will cancel all the running flow runs
m365 flow run cancel --environment $yourPowerPlatformEnvirontmentGUID --flow $yourflowGUID --name $ --confirm
Write-Output "Your flow run cancelled successfully"


    $ErrorMessage = $_.Exception.Message +"in while cancelling Power Automate flow runs!:"

    Write-Host $ErrorMessage -BackgroundColor Red

    Write-Log $ErrorMessage



PnP: Cancel all running flows using PnP PowerShell script

Use the below PnP PowerShell command to cancel all running flows from a given environment:

Example 1:

Stop-PnPFlowRun -Environment $yourPowerPlatformEnvirontmentGUID -Flow $yourflowGUID -Identity $yourflowRunID


  • The above PnP command will cancel the specified flow run of the specified flow

Example 2:

Stop-PnPFlowRun -Environment $yourPowerPlatformEnvirontmentGUID -Flow $yourflowGUID -Identity $yourflowRunID -Force


  • This above PnP command will cancel the specified flow run of the specified flow without confirmation

In order to get the environment id dynamically, we can use the below PnP command:

$environment = Get-PnPPowerPlatformEnvironment

In the above PowerShell and PnP PowerShell, we have used parameters like Environment GUID, Flow GUID, and Flow Run ID. How do I get these values? It looks difficult to get, but don’t worry, it is very simple. It is available on the my flow run history Power Automate page. For this, refer to this article: Power Automate: Get Flow Run ID Dynamically in 2 Ways.

Demo: Cancel All Flow Runs

Once we execute the above cancel all flow runs PowerShell script or PnP PowerShell script successfully, we can see that all instances of the given Power Automate flow have been cancelled.

Cancel All Your Running Power Automate Flow Runs Using PowerShell M365 CLI
Cancel All Your Running Power Automate Flow Runs Using PowerShell M365 CLI

Summary: Cancel Running Flows using PowerShell Script

In this article, we have learned about how to cancel running flows using the PowerShell script and PnP PowerShell. We have also learned what CLI is for Microsoft 365 and how to install the M365 CLI package on the laptop.

See Also: SharePoint PowerShell Articles

You may also visit the PowerShell article hub, where you will see a bunch of articles focusing on PowerShell, like SharePoint PowerShell, PnP PowerShell etc. All the articles are written with real-time project scenarios and troubleshooting techniques.

If you found this article helpful and enjoyed it, please consider sharing it with your friends and colleagues. Please don’t forget to subscribe to our site to receive our latest articles directly in your inbox. 🙂


About Post Author

Do you have a better solution or question on this topic? Please leave a comment