Sub topic 12
Publishing from a script: PowerShell and REST API
This chapter shows how to publish a .pbix file without opening Power BI Desktop. It assumes you can already publish from Desktop, know what a workspace and its roles are, and have a Microsoft work or school account that can sign in to the Power BI service. You should also be comfortable running commands in PowerShell or a Bash shell.
Why script the publish
Publishing by hand works for one report. It becomes a problem when you publish the same file to several workspaces, republish on every change, or want a repeatable step in a build pipeline. A script gives you the same result each time and leaves a record of what ran.
Both approaches below do the same thing the Desktop Publish button does. They upload the .pbix file, and the service creates or updates a report and a semantic model in the workspace. The same rules apply. The account you use needs a workspace role that can publish (Admin, Member, or Contributor), and the workspace needs a suitable license or capacity.
Option 1: the MicrosoftPowerBIMgmt module
Microsoft provides a PowerShell module that wraps the Power BI REST API. Install it once for your user, sign in, and look up the workspace to confirm you can see it.
Install-Module -Name MicrosoftPowerBIMgmt -Scope CurrentUser
Connect-PowerBIServiceAccount
Get-PowerBIWorkspace -Name 'Sales Analytics'Connect-PowerBIServiceAccount opens a sign-in window. If Get-PowerBIWorkspace returns nothing, you are either not a member of that workspace or the name is spelled differently. Fix that before you try to upload.
Next, upload the file. New-PowerBIReport takes the path to the file, the name to give the report, and the target workspace.
Connect-PowerBIServiceAccount
$ws = Get-PowerBIWorkspace -Name 'Sales Analytics'
New-PowerBIReport -Path '.\Sales.pbix' -Name 'Sales' -Workspace $ws -ConflictAction CreateOrOverwriteThe -ConflictAction parameter decides what happens when an item with the same name already exists in the workspace.
| Value | Behavior |
|---|---|
| `Abort` | Stops with an error if a same-named item exists. |
| `Ignore` | Leaves the existing item and uploads the file as a separate item. |
| `Overwrite` | Replaces an existing same-named item and fails if none exists. |
| `CreateOrOverwrite` | Replaces a same-named item if present, otherwise creates a new one. |
CreateOrOverwrite suits a repeatable script because it works on the first run and on every later run.
Option 2: the REST API
If you cannot use PowerShell, or you are working from a pipeline that runs shell commands, call the Imports operation directly. It accepts the file as multipart form data.
curl -X POST "https://api.powerbi.com/v1.0/myorg/groups/$GROUP_ID/imports?datasetDisplayName=Sales&nameConflict=CreateOrOverwrite" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-F "[email protected]"Three pieces matter here:
$GROUP_IDis the workspace ID. In the API, a workspace is called a group. You can read the ID from the workspace URL in the service, or from theIdproperty returned byGet-PowerBIWorkspace.datasetDisplayNameis the name the imported item gets. The name conflict rule above applies throughnameConflict.$ACCESS_TOKENis a Microsoft Entra ID access token for the Power BI service. How you obtain it depends on your setup. For an interactive test,Get-PowerBIAccessTokenfrom the module returns one for the signed-in account.
The import runs asynchronously. A successful call returns an import ID, and the report appears once processing finishes.
Common errors
- 401 Unauthorized. The token is missing, expired, or issued for the wrong resource. Request a new token and retry.
- 403 Forbidden. The account is signed in but does not have a role that can publish to that workspace, or tenant settings block the action. Check the workspace role.
- Workspace not found.
Get-PowerBIWorkspacereturned nothing, so$wsis empty. Check the name and your membership before uploading. - Duplicate items. Using
Ignore, or a different-Name, creates a second report instead of updating the first. UseCreateOrOverwritewith a stable name.
Verify the result
After the upload, list the reports in the workspace and check that yours is there.
Get-PowerBIReport -Workspace $ws -Name 'Sales'Then open the report in the service. A report that exists but shows errors usually points to missing credentials on the semantic model, which you covered in the earlier chapters on credentials and refresh.
Exercise
A nightly script uploads Sales.pbix to a workspace with New-PowerBIReport. On the first night it works. On the second night it fails because the report already exists. Which -ConflictAction value should you use so the script succeeds on both nights, and why?
Show answer
Use CreateOrOverwrite. It creates the report when none exists (first night) and replaces the existing report and semantic model when one does (later nights). Overwrite would fail on the first night because nothing exists to replace, and Abort would fail on the second night.
Exercise
A curl call to the Imports endpoint returns 403 Forbidden, although the token is valid. What should you check first?
Show answer
Check the signed-in account's role in the target workspace. Only Admin, Member, or Contributor can publish, and a Viewer gets a 403. Also check whether tenant settings restrict publishing.