top of page

Duplicating workspaces by using Power BI cmdlets

By using the Power BI management cmdlets, you can automate typical user and administration tasks with significantly less effort and complexity than with direct Power BI REST API calls. To illustrate this point, let’s take a look at Sirui’s original copyWorkspace.ps1 script, published in 2017 for the “Duplicate workspaces using the Power BI REST APIs: a step-by-step tutorial.

This script shows you how to copy a workspace with its dashboards, reports and datasets via REST API calls. In comparison, the updated Power BI cmdlets version, available as a sample in the Open Source repository for Power BI cmdlets on GitHub, accomplishes the same tasks with about 100 fewer lines. The cmdlets make the script far more intuitive and easier to understand and troubleshoot.

The original copyWorkspace.ps1 script performs the following main steps:

  1. Authenticating the Power BI user by using the Active Directory Authentication Library (ADAL).

  2. Exporting and then import PBIX files between source and target workspaces.

  3. Copying any remaining reports built on the same datasets already copied in step 2.

  4. Copying dashboards with their tiles.

The first step has the most complex challenges because it required you to register an Azure AD app, and use a custom function based on ADAL to obtain an auth token that you then had to pass to every subsequent REST API call.

The Power BI cmdlets, on the other hand, enable you to replace all this complexity with a single Login-PowerBI command. You no longer need to register an app and you don’t need to handle auth tokens directly because the Power BI cmdlets already know how to access the auth token in your PowerShell session. Can it be any more straightforward than that?

You also no longer need to construct REST API calls to get to a workspace, its dashboards, reports or datasets. Just use the Get-PowerBIWorkspace cmdlet to access the source and target workspaces, and if the target workspace does not exist, use the New-PowerBIWorkspace cmdlet to create it.

Then call Get-PowerBIReport and Export-PowerBIReport to export the PBIX files from the source workspace, and then the New-PowerBIReport cmdlet to import the exported PBIX files into the target workspace.

Step 3 is also quickly accomplished thanks to the Copy-PowerBIReport cmdlet. Again, no direct REST API calls necessary. Copy-PowerBIReport can copy the remaining reports straight from the source to the target workspace and rebind them to the datasets that already exist in the target.

And finally, simplify Step 4 by using the Get-PowerBIDashboard and New-PowerBIDashboard cmdlets to recreate any dashboards in the target workspace, and use Get-PowerBITile and Copy-PowerBITile to recreate the tiles in each dashboard copy, and the job is done! The following screenshot shows the updated script based on Power BI cmdlets in action:

The PowerShell script now also accepts the names of the source and target workspaces as parameters and a flag to indicate if the target workspace should be created if it doesn’t exist. These parameters make the script operate almost like a cmdlet itself. For example, you could use the command .\CopyWorkspaceNew.ps1 -SourceWorkspaceName “My Workspace” -TargetWorkspaceName “My Workspace Copy” to create a copy of the content, except workbooks and dataflows, from your personal workspace in a new workspace called “My Workspace Copy”.

At this point, you might be wondering how to copy workbooks and dataflows.

Unfortunately, there are no APIs to copy or clones these artifacts yet, and without these APIs it is not possible to build the missing cmdlets. The plan is to add the missing pieces to the Power BI management cmdlets as soon as the APIs become available, so that you can create more complete copies of your workspaces with less manual cleanup after running the script.

Some opinions expressed in this article may be those of a guest author and not necessarily Analytikus. Staff authors are listed in

334 views0 comments


bottom of page