There have been several new Power Automate/Flow commands added to PnP.PowerShell (v1 released this week). In this blog article I will test these new commands out and show you how they can be used in your environment. I will also provide a script at the end to export all Power Automate Flows in your environment to JSON and ZIP format.
PnP PowerShell is a .NET Core 3.1 / .NET Framework 4.6.1 based PowerShell Module providing over 500 cmdlets that work with Microsoft 365 environments and more specifically SharePoint Online and Microsoft Teams. It replaces the old SharePointPnPPowerShellOnline PowerShell module that only runs on Windows and was designed primarily for SharePoint usage.
PnP.PowerShell is cross platform (Windows, Unix, Mac, Linux etc) and runs best on PowerShell 7.
There are some instructions on how to install and authenticate with the new module here.
New Flow Commands
Disclaimer: PnP.PowerShell uses the term Flow for Power Automate so I will be using the wording Flow in this blog, sorry for any confusion 🙂
I will now run a Get-Command in PowerShell to show all of the PnP PowerShell commands that are available for Flow. You can see that there are six new commands:
Connect using Connect-PnPOnline using a user account and the commands will only Flows and Flow environments that your user account has access to.
If the account has either the M365/Azure AD role Power Platform administrator or the Global administrator role admin role then the -AsAdmin parameter can be used which allows you view/use other Flows (that you are not the owner) as an admin.
Get Flow Environments – Get-PnPFlowEnvironment
This command retrieves all of the Flow environments for your tenant.
Currently it only returns the GUIDs, locations and further details of all the Flow environments that your user account has access to. If you want to return the DisplayName of the environment i.e. Personal Productivity, Name of Dataverse for Teams Environment etc. unfortunately at the moment this is hidden in the properties object.
I can use the following one liner with this command to also return the DisplayNames information from Properties.
Get-PnPFlowEnvironment | Select-Object Name, Location, Id -ExpandProperty Properties
PnP.PowerShell Documentation Link: Get-PnPFlowEnvironment
Get Flow – Get-PnPFlow
This command can either retrieve all the Flows in a environment or can be used to target single flow in an environment using the -Identity parameter. This retrieves the Flows for the current user and uses the parameter -AsAdmin to return the flows as an admin (if your the user account has the relevant admin permissions).
Below I will show an example using Get-PnPFlow and retrieving all the Flows in an environment.
Get-PnPFlow -Environment <Name of Environment here>
The command below returns a specific flow (using Name/Id).
Get-PnPFlow -Environment "Default-d2af3166-e76b-4d03-9942-fbc79d6b69f9" -Identity <Name or Id of Flow> -AsAdmin
PnP.PowerShell Documentation Link: Get-PnPFlow
Disable Flow – Disable-PnPFlow
This command allows you to disable a specific flow and will be handy so you disable Flows using a PowerShell command rather than using the UI. Again use the parameter -AsAdmin to disable the flow as an admin (if your user account has the relevant admin permissions).
Disable-PnPFlow -Environment <Name of Environment here> -Identity <Name or Id of Flow> -AsAdmin
Below we can see the command in action disabling a Flow. We use the command Get-PnPFlow to show the status of the Flow before and after the command is run.
PnP.PowerShell Documentation Link: Disable-PnPFlow
Enable Flow – Enable-PnPFlow
This command is the reverse of the Disable-PnPFlow and allows you to enable a specific flow using a PowerShell command. Again use the parameter -AsAdmin to enable the flow as an admin (if your user account has the relevant admin permissions).
Enable-PnPFlow -Environment <Name of Environment here> -Identity <Name or Id of Flow> -AsAdmin
Below we can see the command in action enabling a Flow. We use the command Get-PnPFlow to show the status of the Flow before and after the command is run.
PnP.PowerShell Documentation Link: Enable-PnPFlow
Remove Flow – Remove-PnPFlow
This command allows you to delete/remove a Flow using the PowerShell command Remove-PnPFlow. Again use the parameter -AsAdmin to remove the flow as an admin (if your user account has the relevant admin permissions).
Remove-PnPFlow -Environment <Name of Environment here> -Identity <Name or Id of Flow> -AsAdmin
Below we can see the command in action removing a Flow and a confirmation to confirm the removal of a the Flow (use -Force to not receive this confirmation).
PnP.PowerShell Documentation Link: Remove-PnPFlow
Export Flow – Export-PnPFlow
This command allows you to export a specific flow to the console, JSON or as a zip package. This command is handy for when wanting to export Flows to Azure Logic Apps when the option in the export dropdown for Logic Apps template (.json) is not available in Power Automate.
Export-PnPFlow -Environment -Environment <Name of Environment here> -Identity <Name or Id of Flow>
Below we can see the command in action exporting the Flow in JSON format to the console.
To export the Flow to a JSON file on your computer so it can be used in Azure Logic Apps I use the following command where I am piping the results to | Out-File “Output.json”.
Export-PnPFlow -Environment <Name of Environment here> -Identity <Name or Id of Flow> | Out-File "Outputs.json"
I’m looking forward to one day when there maybe an Add-PnPFlow command which we could use to add flows to an environment using the JSON we have just extracted.
PnP.PowerShell Documentation Link: Export-PnPFlow
Bonus: Script to Export all Power Automate Flows using PnP.PowerShell
The Flow functionality has recently been added to PnP.PowerShell and follows on from the CLI for Microsoft 365 team adding this functionality to their product. Erwin van Hunen had now added this functionality to PnP PowerShell.
The script below borrows from Garry Trinder’s blog where he provides a script to export all your flows using CLI for Microsoft 365 – link here. I have translated this to use PnP.PowerShell but all credit should go to Garry for the logic!
So there we have it we have been through and used all of the new PnP.PowerShell commands for flows and provided a few working examples, use cases and tips.
These are a welcome addition to the PnP.PowerShell commands and are very helpful for scripted management of environments i.e. viewing all flows, disabling flows, removing flows, backing up all flows etc. I’m looking forward to this functionality being further developed – wouldn’t it be great to add and modify flows using PnP.PowerShell?