Triggering Power Automate Flows from PnP PowerShell

Loading

Power Automate (formerly Microsoft Flow) allows you to automate business processes across SharePoint Online, Office 365, and other services. With PnP PowerShell, you can trigger Power Automate flows programmatically, making it useful for bulk processing, scheduled tasks, and integrations.

This guide will walk you through step-by-step how to trigger a Power Automate flow using PnP PowerShell.


Step 1: Install and Connect to SharePoint Online

Before running any PnP PowerShell commands, install PnP PowerShell:

Install-Module -Name PnP.PowerShell -Scope CurrentUser -Force

Then, connect to your SharePoint Online site:

$SiteUrl = "https://yourtenant.sharepoint.com/sites/yoursite"
Connect-PnPOnline -Url $SiteUrl -Interactive

🔹 Replace yourtenant with your tenant name
🔹 Replace yoursite with your SharePoint site name

Successfully connected to SharePoint!


Step 2: Get Available Power Automate Flows

To check all available Power Automate flows, use:

Get-PnPFlow

This will list all flows associated with your tenant or SharePoint site.

Flows retrieved!


Step 3: Get the Flow ID

To trigger a specific flow, you need its Flow ID. Run:

Get-PnPFlow | Select DisplayName, Id

This will return a list like:

DisplayName                   Id
------------ ------------------------------------
Approval Workflow 123abc45-6789-def0-gh12-345678ijklmn
Document Review Process 789xyz12-3456-abcd-ef78-90123ghijkl

Copy the ID of the flow you want to trigger.

Flow ID obtained!


Step 4: Trigger the Power Automate Flow

Now, trigger the flow using Invoke-PnPFlow:

$FlowId = "123abc45-6789-def0-gh12-345678ijklmn"  # Replace with your flow's ID
Invoke-PnPFlow -Identity $FlowId

Flow successfully triggered!


Step 5: Trigger a Flow with Parameters (Advanced)

Some flows require input parameters, like an approval request or a document URL.
Use the Parameters option:

$FlowId = "123abc45-6789-def0-gh12-345678ijklmn"

$Params = @{
"Requester" = "user@domain.com"
"DocumentURL" = "https://yourtenant.sharepoint.com/sites/yoursite/Shared Documents/sample.docx"
}

Invoke-PnPFlow -Identity $FlowId -Parameters $Params

Flow triggered with parameters!


Step 6: Trigger a Flow on a SharePoint List Item

If your flow is linked to a SharePoint list, trigger it when a new item is added:

$ListName = "ApprovalRequests"
$ItemId = 1 # Replace with actual list item ID

Invoke-PnPFlow -List $ListName -Identity $FlowId -ItemId $ItemId

Flow triggered for List Item ID 1!


Step 7: Automate Flow Execution with Task Scheduler

To trigger the flow automatically every day, create a PowerShell script:

$SiteUrl = "https://yourtenant.sharepoint.com/sites/yoursite"
Connect-PnPOnline -Url $SiteUrl -Interactive
$FlowId = "123abc45-6789-def0-gh12-345678ijklmn"
Invoke-PnPFlow -Identity $FlowId
Write-Host "Flow triggered successfully!"

Schedule the script:

1️⃣ Save this script as TriggerFlow.ps1
2️⃣ Open Task Scheduler
3️⃣ Create a new Basic Task
4️⃣ Set it to run daily or at specific intervals
5️⃣ In the Action, select Start a Program and enter:

powershell.exe -File "C:\Path\To\TriggerFlow.ps1"

6️⃣ Click Finish

Flow will now trigger automatically!


Step 8: Monitor Flow Execution

To check flow execution logs:

Get-PnPFlowRun -Identity $FlowId

Execution logs retrieved!


Step 9: Managing Power Automate Flows using PnP PowerShell

List all Flows

Get-PnPFlow

Enable or Disable a Flow

Set-PnPFlowState -Identity $FlowId -State Disabled  # Disable the flow
Set-PnPFlowState -Identity $FlowId -State Enabled # Enable the flow

Delete a Flow

Remove-PnPFlow -Identity $FlowId

Flow management simplified!

Leave a Reply

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