Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
GarethNW
New Member

Problem publishing a .pbix file to PBI Workspace using the rest api and PowerShell

I am trying to publish a Power BI Report via a PowerShell script. Has anyone had much experience with this as I can't quite get it to work using this method.

 

 

$clientId = "****"
$clientSecret = "****"
$tenantId = "****"
$apiUrl = "https://api.powerbi.com/v1.0/myorg/groups/{Workspace}/imports?datasetDisplayName={DatadetName}"

$pbixFilePath = ".\BIDeploymentPlan.pbix"

$resourceUrl = "https://analysis.windows.net/powerbi/api"

$tokenBody = @{
    grant_type    = "client_credentials"
    client_id     = $clientId
    client_secret = $clientSecret
    resource      = $resourceUrl
    scope         = "https://graph.microsoft.com/.default"
}

$tokenResponse = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$tenantId/oauth2/token" `
    -Method POST -Body $tokenBody

$accessToken = $tokenResponse.access_token

$headers = @{
    "Authorization" = "Bearer $accessToken"
}

$body = @{
    filePath = $pbixFilePath
} | ConvertTo-Json -Depth 1

$restMethodParams = @{
    Uri         = $apiUrl
    Method      = 'Post'
    Headers     = $headers
    Body        = $body
    ContentType = 'application/json'
}

Invoke-RestMethod @restMethodParams

 

 

I have used the following document to build this script: https://learn.microsoft.com/en-us/rest/api/power-bi/

Thank you for any help you can offer.

No issue with my access token as the following code works fine: Invoke-RestMethod -Uri $apiUrl -Method Delete -Headers $headers

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @GarethNW ,

 

I suggest you to refer to below code to publish your pbix file to Power BI Service by PowerShell.

 

$clientID = "<ClientID>"

$filePath = "C:\Users\...\MyPBIXTest.pbix" 

$groupname = "WorkspaceName"

$datasetDisplayName = "MyImportTest" 

$nameConflict = "Overwrite"

$preferClientRouting = "false"

 

#Get Token

$authToken = Get-PBIAuthToken -ClientId $clientID -Credential (Get-Credential)

 

#Get Group ID

$group = Get-PBIGroup -name $groupname

 

#Set URI for Import call

$powerBIUri = "https://api.powerbi.com/v1.0/myorg/"

$uri = "groups/$($group.id)/imports?datasetDisplayName=$datasetDisplayName&preferClientRouting=$preferClientRouting"

 

# Initializing

$body = ""

$contentType = $null

 

#Setting boundary for string

$boundary = [System.Guid]::NewGuid().ToString("N")

$LF = [System.Environment]::NewLine

 

#Getting content using the file path

$fileName = [uri]::EscapeDataString([IO.Path]::GetFileName($filePath))

$fileBin = [IO.File]::ReadAllBytes($filePath)

$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")

$fileEnc = $enc.GetString($fileBin)

 

#Set body

$body = (

"--$boundary",

"Content-Disposition: form-data; name=`"file0`"; filename=`"$fileName`"; filename*=UTF-8''$fileName",

"Content-Type: application/x-zip-compressed$LF",

$fileEnc,

"--$boundary--$LF"

) -join $LF

 

#Set content type

$contentType = "multipart/form-data; boundary=--$boundary"

 

#Set API URL

$restApiUrl = $powerBIUri + $uri

 

#Set Header

$authHeader = @</span>{

'Content-Type'='application/json'

'Authorization'="Bearer $authToken"}

 

 

# Invoke REST Method

$response = Invoke-RestMethod -Uri $restApiUrl -Headers $authHeader -Body $body -Method "POST" -contentType $contentType -Verbose

 

Make sure your PowerShell is installed correctly.

For reference: Power BI Cmdlets reference | Microsoft Learn

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

View solution in original post

1 REPLY 1
Anonymous
Not applicable

Hi @GarethNW ,

 

I suggest you to refer to below code to publish your pbix file to Power BI Service by PowerShell.

 

$clientID = "<ClientID>"

$filePath = "C:\Users\...\MyPBIXTest.pbix" 

$groupname = "WorkspaceName"

$datasetDisplayName = "MyImportTest" 

$nameConflict = "Overwrite"

$preferClientRouting = "false"

 

#Get Token

$authToken = Get-PBIAuthToken -ClientId $clientID -Credential (Get-Credential)

 

#Get Group ID

$group = Get-PBIGroup -name $groupname

 

#Set URI for Import call

$powerBIUri = "https://api.powerbi.com/v1.0/myorg/"

$uri = "groups/$($group.id)/imports?datasetDisplayName=$datasetDisplayName&preferClientRouting=$preferClientRouting"

 

# Initializing

$body = ""

$contentType = $null

 

#Setting boundary for string

$boundary = [System.Guid]::NewGuid().ToString("N")

$LF = [System.Environment]::NewLine

 

#Getting content using the file path

$fileName = [uri]::EscapeDataString([IO.Path]::GetFileName($filePath))

$fileBin = [IO.File]::ReadAllBytes($filePath)

$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")

$fileEnc = $enc.GetString($fileBin)

 

#Set body

$body = (

"--$boundary",

"Content-Disposition: form-data; name=`"file0`"; filename=`"$fileName`"; filename*=UTF-8''$fileName",

"Content-Type: application/x-zip-compressed$LF",

$fileEnc,

"--$boundary--$LF"

) -join $LF

 

#Set content type

$contentType = "multipart/form-data; boundary=--$boundary"

 

#Set API URL

$restApiUrl = $powerBIUri + $uri

 

#Set Header

$authHeader = @</span>{

'Content-Type'='application/json'

'Authorization'="Bearer $authToken"}

 

 

# Invoke REST Method

$response = Invoke-RestMethod -Uri $restApiUrl -Headers $authHeader -Body $body -Method "POST" -contentType $contentType -Verbose

 

Make sure your PowerShell is installed correctly.

For reference: Power BI Cmdlets reference | Microsoft Learn

 

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.