Cancel multiple running instances of a Cloud Flow using PowerShell
Sometimes we have requirements where we have to automate some process using Power Automate, this could be something very simple like sending an email to the customer when the Order is ready to be dispatched, up to the infinite complex scenarios that we could have in our projects.
And it is normal to make mistakes when we are building these cloud flows, after all making mistakes is one of the best ways to learn. One of these mistakes can be to build a cloud flow that causes an infinite loop.
This is a nasty problem that we don’t want to have, because if we don’t realize it, it could be running for hours or even days causing API throttling and hitting limits for calling Dataverse.
Suppose this infinite loop is like the following: the cloud flow is triggered when the contact address is updated and at the end the same cloud flow updates the contact status, and of course we don’t have any filter on the trigger action.
Now let’s say we realize of this issue and then we go and turn off the cloud flow, the next problem we will have is the hundreds or maybe thousands of running instances of that cloud flow. Cancel them manually one by one seems like a punishment that no one will do.
I don’t know if it’s happened to you, but sh*t happens sometimes, so here’s the best way to cancel all those running instances using PowerShell.
Installing CLI for Microsoft 365
Using the CLI for Microsoft 365, you can manage your Microsoft 365 tenant and SharePoint Framework projects but it also helps you with the Power Platform. The CLI for Microsoft 365 is distributed as an NPM package. To use it, install it globally using:
npm i -g @pnp/cli-microsoft365
So you can open a command line and enter the command:
After a few seconds you should see something like:
Also you can check the version running the following command:
Creating PowerShell Script
Create a file with the extension ps1 (in my case I gave it the name StopRunningFlows.ps1) and this is the code to put inside the file:
Getting the Environment and the Flow Guid
You have to go to Power Automate and open your flow as if you were editing it, in the URL you will see both GUIDs:
Login to M365 and executing the script
Open PowerShell in admin mode, you can run m365 status to check if you are logged in already or not:
If that is the case then run m365 login command, now You will see a message like this: “To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code <here you will see the code> to authenticate.” – Do exactly that. Copy that code, open the URL and paste the code.
Then you will have to pick the right account and enter your password:
Click in Continue:
Finally go back to PowerShell and you will see the login process was successful:
Now in PowerShell change the path where the ps1 file is located and run the command replacing both parameters with yours: ./StopRunningFlows.ps1 <EnvironmentGuid> <CloudFlowGuid>
In some cases, after attempting to execute the above command, this problem may occur:
.ps1 is not digitally signed. The script will not execute on the system.
To fix it you have to run the command below to run Set-ExecutionPolicy and change the Execution Policy setting:
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
Finally try to execute the command (./StopRunningFlows.ps1 <EnvironmentGuid> <CloudFlowGuid>) again and now you should start to see something like the following: