Power BI has become an essential tool for business intelligence and reporting.
However, managing Power BI reports across different environments or workspaces can be time-consuming and error-prone.
PowerShell offers an efficient and reliable method to automate the export and import of Power BI reports.
In this blog post, we’ll walk through how to use PowerShell to export Power BI reports from one workspace and import them into another. We’ll also cover how to update dataset parameters and refresh datasets after the import.
To interact with Power BI, you need to authenticate. There are multiple ways to authenticate:
Connect-PowerBIServiceAccount
$username = “” $password = “” | ConvertTo-SecureString -asPlainText -Force $credential = New-Object System.Management.Automation.PSCredential($username, $password) Connect-PowerBIServiceAccount -Credential $credential
$clientId = "your-client-id" $clientSecret = "your-client-secret" | ConvertTo-SecureString -AsPlainText -Force $tenantId = "your-tenant-id" $credential = New-Object System.Management.Automation.PSCredential($clientId, $clientSecret) Connect-PowerBIServiceAccount -ServicePrincipal -TenantId $tenantId -Credential $credential
Once authenticated, you can export reports from a specific workspace. Here’s how:
1. Retrieve the Workspace:
$workspaceId = “your-workspace-id”
$workspace = Get-PowerBIWorkspace -Id $workspaceId
2. Export Reports:
Specify the export path and export the reports. $exportPath = "C:TempPowerBIExports" if (!(Test-Path -Path $exportPath)) { New-Item -ItemType Directory -Path $exportPath } $reports = Get-PowerBIReport -WorkspaceId $workspace.Id foreach ($report in $reports) { $filePath = Join-Path -Path $exportPath -ChildPath "$($report.Name).pbix" Export-PowerBIReport -WorkspaceId $workspace.Id -ReportId $report.Id -OutFile $filePath }
Step 3: Import Power BI Reports
After exporting the reports, you can import them into a new workspace.
1. Create a New Workspace:
$newWorkspace = New-PowerBIWorkspace -Name “New Workspace”
2. Import Reports: Import the reports from the export path.
$reportsToImport = Get-ChildItem -Path $exportPath -Filter "*.pbix" foreach ($reportFile in $reportsToImport) { $filePath = Join-Path -Path $exportPath -ChildPath $reportFile.Name New-PowerBIReport -Path $filePath -Workspace $newWorkspace }
Step 4: Update Dataset Parameters and Refresh
After importing the reports, you may need to update the dataset parameters and refresh the datasets.
1. Update Dataset Parameters:
$datasetId = "your-dataset-id" $body = '{ "updateDetails": [ { "name": "EnvironmentURL", "newValue": "your-new-value" } ] }' Invoke-PowerBIRestMethod -Url "/datasets/$datasetId/Default.UpdateParameters" -Method Post -Body $body
2. Refresh Dataset:
Invoke-PowerBIRestMethod -Method Post -Url “groups/$($newWorkspace.Id)/datasets/$datasetId/refreshes”
Step 5: Update Data Source Credentials You must update the credentials if the imported reports rely on data sources.
1. Retrieve Data Sources:
$datasources = Get-PowerBIDatasource -WorkspaceId $newWorkspace.Id -DatasetId $datasetId
2. Update Credentials:
foreach ($datasource in $datasources) { $body = @{ "credentialDetails" = @{ "credentialType" = "OAuth2" "encryptedConnection" = "Encrypted" "useCallerAADIdentity" = $true } } $jsonBody = $body | ConvertTo-Json -Depth 5 Invoke-PowerBIRestMethod -Url "https://api.powerbi.com/v1.0/myorg/gateways/$($datasource.GatewayId)/datasources/$($datasource.DatasourceId)" -Method PATCH -Body $jsonBody }
By automating the export and import of Power BI reports using PowerShell, you can save time and reduce the risk of errors. This approach is particularly useful when managing multiple workspaces or environments. Whether you’re migrating reports or setting up new environments, PowerShell provides a flexible and powerful way to streamline the process.
The post Automating Power BI Report Export and Import Using PowerShell first appeared on Microsoft Dynamics 365 CRM Tips and Tricks.
Original Post https://www.inogic.com/blog/2025/05/automating-power-bi-report-export-and-import-using-powershell/