Importing a Flow Package using PowerShell

Loading

Importing a Power Automate Flow package using PowerShell is useful for migrating, restoring, or deploying flows across environments. This guide will take you through the step-by-step process of importing a Flow package into your Power Platform environment.


Step 1: Install Required PowerShell Modules

Before importing a flow, ensure 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 the installation.


Step 2: Authenticate to Power Platform

To interact with Power Automate, sign in with admin credentials:

Add-PowerAppsAccount

Alternatively, if using Microsoft Graph, connect with:

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

Step 3: Identify the Target Environment

To import the flow, you must specify the target environment. Use the following command to list all environments and find the correct Environment ID:

Get-AdminEnvironment | Select-Object DisplayName, EnvironmentName | Format-Table -AutoSize

🔹 Note the Environment Name where you want to import the Flow.


Step 4: Import the Flow Package

Once you have the Environment ID and Flow Package (.zip file), use the following command to import the flow:

$EnvironmentName = "<TargetEnvironmentID>"  # Replace with your environment ID
$ImportPath = "C:\PowerAutomate\FlowPackage.zip" # Path to the exported package

Import-AdminFlow -EnvironmentName $EnvironmentName -PackageFilePath $ImportPath

Write-Host "Flow package has been successfully imported into '$EnvironmentName'."

This command imports the flow from the ZIP package into the specified environment.


Step 5: Verify the Imported Flow

After importing, verify that the flow is available:

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

🔹 The imported flow should now be listed in the target environment.


Step 6: Import Multiple Flows in Bulk

If you have multiple flow packages to import, use the following script:

$EnvironmentName = "<TargetEnvironmentID>"
$ImportFolder = "C:\PowerAutomate\Imports"

# Get all ZIP files in the folder
$Packages = Get-ChildItem -Path $ImportFolder -Filter "*.zip"

foreach ($Package in $Packages) {
$ImportPath = $Package.FullName
Import-AdminFlow -EnvironmentName $EnvironmentName -PackageFilePath $ImportPath
Write-Host "Imported '$($Package.Name)' into '$EnvironmentName'."
}

Write-Host "All flow packages imported successfully!"

This script:
Loops through all ZIP files in the folder
Imports each package automatically


Step 7: Assign Permissions to the Imported Flow

By default, imported flows may not have the correct permissions. Assign ownership using:

$FlowName = "<ImportedFlowName>"  # Replace with the imported Flow's ID
$UserEmail = "<UserEmail>" # Replace with the user's email

Set-AdminFlowOwnerRole -EnvironmentName $EnvironmentName -FlowName $FlowName -PrincipalObjectId (Get-AdminFlowOwnerRole -EnvironmentName $EnvironmentName -FlowName $FlowName).PrincipalObjectId -RoleName CanEdit

Write-Host "Assigned 'CanEdit' role to $UserEmail for Flow: $FlowName."

🔹 This ensures that the right user can manage the flow after import.


Step 8: Enable the Imported Flow

Imported flows might be disabled by default. To enable the flow:

Enable-AdminFlow -EnvironmentName $EnvironmentName -FlowName $FlowName

Write-Host "Flow '$FlowName' has been enabled."

🔹 If you want to disable a flow, use:

powershellCopyEditDisable-AdminFlow -EnvironmentName $EnvironmentName -FlowName $FlowName

Step 9: Automate the Import Process

If you frequently import flows, schedule an automated task:

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

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

🔹 This will automate flow imports every day at 3 AM.


Step 10: 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 *