Automating Power BI Report Export using PowerShell

Loading

Exporting Power BI reports manually can be time-consuming, especially for frequent tasks. Automating report exports using PowerShell allows you to schedule, manage, and retrieve reports efficiently.

This guide will show you step-by-step how to automate Power BI report exports using PowerShell.


Step 1: Prerequisites

1. Install Power BI PowerShell Module

Ensure the Microsoft Power BI Management module is installed:

Install-Module -Name MicrosoftPowerBIMgmt -Scope CurrentUser -Force

2. Connect to Power BI Service

Authenticate with Power BI:

Connect-PowerBIServiceAccount

You’ll be prompted to sign in with your Power BI credentials.


Step 2: Get Report and Workspace Details

To export a report, you need:
1️⃣ Workspace ID
2️⃣ Report ID

Run the following command to list all workspaces:

Get-PowerBIWorkspace -Scope Organization | Select-Object Id, Name

Find the workspace name and note the corresponding ID.

Next, list all reports in a workspace:

$workspaceId = "<Your_Workspace_ID>"
Get-PowerBIReport -WorkspaceId $workspaceId | Select-Object Id, Name

Find the report name and note the corresponding ID.

Workspace and Report IDs retrieved!


Step 3: Export Power BI Report to PDF

Use the following script to export the report as a PDF:

$workspaceId = "<Your_Workspace_ID>"
$reportId = "<Your_Report_ID>"

# Export report as PDF
$exportResult = Invoke-PowerBIRestMethod -Url "groups/$workspaceId/reports/$reportId/ExportTo" -Method Post -Body (@{} | ConvertTo-Json)

Write-Host " Export request submitted!"

This triggers the export process in Power BI.


Step 4: Monitor Export Progress

After starting the export, monitor its status:

$exportId = "<Export_Job_ID>"  # Replace with actual job ID from Step 3

$exportStatus = Invoke-PowerBIRestMethod -Url "groups/$workspaceId/reports/$reportId/exports/$exportId" -Method Get

$exportStatus | ConvertFrom-Json

If status is "Succeeded", the export is ready to download.


Step 5: Download Exported Report

Once the export is complete, download the report:

$exportFileUrl = "<Export_File_URL>"  # URL retrieved from Step 4

Invoke-WebRequest -Uri $exportFileUrl -OutFile "C:\Exports\PowerBI_Report.pdf"

Write-Host " Report downloaded successfully!"

Step 6: Automate with Scheduled Task

1. Create a PowerShell Script (ExportPowerBIReport.ps1)

# Connect to Power BI
Connect-PowerBIServiceAccount

# Define Workspace and Report IDs
$workspaceId = "<Your_Workspace_ID>"
$reportId = "<Your_Report_ID>"

# Start Export
$exportResult = Invoke-PowerBIRestMethod -Url "groups/$workspaceId/reports/$reportId/ExportTo" -Method Post -Body (@{} | ConvertTo-Json)
$exportId = $exportResult.id

# Monitor Export Progress
do {
Start-Sleep -Seconds 5
$exportStatus = Invoke-PowerBIRestMethod -Url "groups/$workspaceId/reports/$reportId/exports/$exportId" -Method Get | ConvertFrom-Json
} while ($exportStatus.status -ne "Succeeded")

# Download Report
Invoke-WebRequest -Uri $exportStatus.resourceLocation -OutFile "C:\Exports\PowerBI_Report.pdf"

Write-Host " Report export completed successfully!"

2. Schedule PowerShell Script

1️⃣ Open Task Scheduler
2️⃣ Click “Create Basic Task”
3️⃣ Set trigger: Daily at a specified time
4️⃣ Set action: Run PowerShell script

powershell.exe -ExecutionPolicy Bypass -File "C:\Exports\ExportPowerBIReport.ps1"

Automated Power BI report export is now scheduled!

Leave a Reply

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