Share Multiple Power Automate Flows at Once Using PowerShell

Step 1. Before you can start automating the sharing of Power Automate flows using PowerShell, you first need to install the necessary PowerShell modules that allow you to manage Power Apps and Power Automate environments.

The module Microsoft.PowerApps.Administration.PowerShell is crucial for managing Power Automate flows, including tasks like sharing and administration, through PowerShell.

This is before all steps image.

Open PowerShell as Administrator

Run the Install-Module Command: Use the following command to install the module.

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

Step 2. Then, the next step is to retrieve all the Power Automate flows in your environment and export the details into a CSV file. This will allow you to review all flows and later process them in bulk for sharing or other actions. this step you can ignore this for only get the workflow details.

Connections

# Get flow details
$getFlowDetails = Get-AdminFlow
# Initialize an empty array to store flow details
$flowDetailsArray = @()
# Loop through each flow and add its details to the array
$getFlowDetails | ForEach-Object {
    $flowDetailsArray += [PSCustomObject]@{
        DisplayName = $_.DisplayName
        FlowName    = $_.FlowName
        # Add more properties as needed
    }
}
# Export the array to a CSV file
$csvFilePath = "D:\SharePoint\PowerShell\FlowDetails.csv"  # Adjust the file path as needed
$flowDetailsArray | Export-Csv -Path $csvFilePath -NoTypeInformation

Step 3. To retrieve a user's Office 365 ID, follow the steps below.

# Install-Module -Name AzureAD -Force -AllowClobber  if not available 
Connect-AzureAD
$user = Get-AzureADUser -ObjectId "<user-email>"  # Provide the user email ID
$user.UserPrincipalName  # Output: [email protected]
$user.ObjectId          # Output: 12345678-abcd-1234-abcd-12345678abcd

Step 4. To proceed, we need to provide the user's Office 365 ID, which we obtained earlier. Additionally, we must include the environment ID, which can be found in the Power Automate URL.

Now, we will execute the script provided below. This script will be shared with the selected user.

# Connect to Azure AD (make sure you're authenticated)
Connect-AzureAD

# Get all flows (can adjust for specific environment if needed)
$getFlowDetails = Get-AdminFlow

# Loop through each flow and set the owner role for the specified user
$getFlowDetails | ForEach-Object {
    # Get the UserPrincipalName (or ObjectId) of the user you want to assign
    $UserPrincipalObjectId = "<user-object-id>"  # Replace with the actual ObjectId or UserPrincipalName
    
    # The flow name and environment (you may already have this from elsewhere)
    $FlowName = $_.FlowName
    $EnvironmentName = "<your-environment-name>"  # Replace with the actual environment name
    
    # Set the role (CanEdit) for the user on the flow
    Set-AdminFlowOwnerRole -PrincipalType User -PrincipalObjectId $UserPrincipalObjectId -RoleName CanEdit -FlowName $FlowName -EnvironmentName $EnvironmentName
    
    # Output the result for verification
    Write-Host "Assigned 'CanEdit' role to user $UserPrincipalObjectId for flow: $FlowName in environment: $EnvironmentName" -ForegroundColor Green
}

# Optionally, export results to CSV if you need to log the operation
$flowDetailsArray = $getFlowDetails | ForEach-Object {
    [PSCustomObject]@{
        FlowName        = $_.FlowName
        EnvironmentName = $EnvironmentName
        AssignedUser    = $UserPrincipalObjectId
        RoleAssigned    = 'CanEdit'
    }
}

# Export to CSV (optional)
$csvFilePath = "D:\SharePoint\PowerShell\FlowRoleAssignments.csv"
$flowDetailsArray | Export-Csv -Path $csvFilePath -NoTypeInformation

Step 5. You can now review the workflow and share it with a specific user for collaboration.

Details