Exporting a Flow as a Package using PowerShell

Loading

Exporting a Power Automate Flow as a package using PowerShell allows administrators to back up, migrate, or share flows across environments. This is useful for disaster recovery, environment transitions, and deployment automation.


Step 1: Install Required PowerShell Modules

Ensure that the necessary Power Automate PowerShell modules are installed:

Install-Module -Name Microsoft.PowerApps.Administration.PowerShell -Force -AllowClobber
Install-Module -Name Microsoft.PowerApps.PowerShell -Force -AllowClobber

If prompted, press Y to confirm installation.


Step 2: Authenticate to Power Platform

To interact with Power Automate, you need to authenticate using an administrator account:

Add-PowerAppsAccount

If using Microsoft Graph, authenticate with:

Connect-MgGraph -Scopes "User.Read.All", "Application.Read.All"

Step 3: Identify the Flow to Export

Before exporting, list all flows in an environment to get the required Flow Name and ID:

$EnvironmentName = "<EnvironmentID>"  # Replace with your environment ID

Get-AdminFlow -EnvironmentName $EnvironmentName | Select-Object DisplayName, FlowName | Format-Table -AutoSize

This will output:
Flow Display Name
Flow Unique ID


Step 4: Export the Flow as a ZIP Package

Once you have the Flow Name, export it using the following PowerShell command:

$EnvironmentName = "<EnvironmentID>"  # Replace with your environment ID
$FlowName = "<FlowName>" # Replace with your Flow ID
$ExportPath = "C:\PowerAutomate\ExportedFlow.zip" # Desired export location

Export-AdminFlow -EnvironmentName $EnvironmentName -FlowName $FlowName -PackageType Asynchronous -PackageFilePath $ExportPath

Write-Host "Flow '$FlowName' has been exported successfully to '$ExportPath'."

This command exports the flow as a ZIP package, which can be imported into another environment.


Step 5: Export Multiple Flows at Once

To export all flows in an environment, use:

$EnvironmentName = "<EnvironmentID>"
$ExportFolder = "C:\PowerAutomate\Exports"

# Ensure the export directory exists
if (!(Test-Path -Path $ExportFolder)) {
New-Item -ItemType Directory -Path $ExportFolder
}

# Get all flows and export them
$flows = Get-AdminFlow -EnvironmentName $EnvironmentName
foreach ($flow in $flows) {
$ExportPath = "$ExportFolder\$($flow.DisplayName).zip"
Export-AdminFlow -EnvironmentName $EnvironmentName -FlowName $flow.FlowName -PackageType Asynchronous -PackageFilePath $ExportPath
Write-Host "Exported '$($flow.DisplayName)' to '$ExportPath'."
}

Write-Host "All flows exported successfully!"

This script:
Creates a folder for exports
Loops through all flows and saves them individually
Names each exported file with the Flow’s Display Name


Step 6: Verify the Exported Flow Package

To confirm the exported package, navigate to the export folder:

Get-ChildItem -Path "C:\PowerAutomate\Exports"

You should see the ZIP files for exported flows.


Step 7: Automate Flow Exports on a Schedule

To automate daily exports, create a scheduled task using PowerShell:

$ScriptPath = "C:\PowerAutomate\ExportFlows.ps1"

$Trigger = New-ScheduledTaskTrigger -Daily -At 2am
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File $ScriptPath"
Register-ScheduledTask -TaskName "DailyFlowBackup" -Trigger $Trigger -Action $Action -RunLevel Highest -User "NT AUTHORITY\SYSTEM"

This will:
Run the export script daily at 2 AM
Ensure flows are backed up automatically


Step 8: Disconnect PowerShell Session

Once done, disconnect your session:

Disconnect-MgGraph
Disconnect-PowerAppsAccount

Leave a Reply

Your email address will not be published. Required fields are marked *