TS Logo
Published on

Replace Images in Multiple PowerBI Reports using Powershell

Authors
  • avatar
    Name
    Tom Senior
    Twitter
    mailtom@tomsenior.netlinkedintom-senior

Problem

We had a group of 15 or so Microsoft PowerBI reports all with the same 10 pages, each page all had the same image (a logo) on. When that logo needs updating you have to go into each report PBIX manually, change the logo for every page then republish the report... how much time is that going to take?

Surprised to find no simple update option for this built into PowerBI Desktop, the plan was to find a way around this.

Solution

Finding the image file

First point of call was the PBIX file. As the majority of Microsoft's modern file formats are just compressed archives, using 7zip to take a look inside a PBIX file shows the contents of the file. A quick explore through the file, if you follow Reports > StaticResources > RegisteredResources in there you'll see your logo image file, likley with its original image name followed by a bunch of random numbers.

inside-pbix-file

A great result.

Replacing the image for 1 PBIX

If you only have one PBIX file but multiple pages, you can go ahead and replace that image file with your new image keeping the name exactly the same and that's your job done, close the archive, open the PowerBI file and you'll find your lovely new image.

Replacing the image for 10, 15 or even 100 PBIX files

If you need to complete this for more than 5 PBIX files, then its going to be worth time automating. Lets introduce a Powershell script. For this, we'll need all PBIX files in 1 folder that we can search, they can be in subfolders within that original folder.

Secondly we need to create a folder structure with the new image inside that we will copy into the PBIX file. So in C:\Temp create the Reports > StaticResources > RegisteredResources folders and in that final folder, place your new image with the same name as the image in the PBIX, like below.

CDrive-Temp-Folder

Here's the full script with comments to explain whats going on.

$7zipPath = "C:\Program Files\7-Zip\7z.exe" #We are still using 7Zip, so this is the path to your 7z.exe file.
Set-Alias Start-SevenZip $7zipPath #Setting an alias for 7zip to make it look pretty

$SearchPath = "C:\Users\thoma\Documents\PowerBI" #This is your folder containing the PBIX's. They can be in subfolders too as we will recurse through in the next step
$FoundFiles = Get-ChildItem $SearchPath -recurse -Include *.pbix # Search your folder for all PBIX files and store that in $FoundFiles
$Result=@() #A result object for later

foreach($file in $FoundFiles) { #Loop through every file found in the search
    $TempDestination = "C:\Temp\$($file.Name)" #set $TempDestination to the C:\Temp folder + file name
    try {
    Copy-Item -Path $file.FullName -Destination $TempDestination #Copy the PBIX to your C:\Temp folder
    Start-SevenZip d $TempDestination Logo7111830555559804.png -r # Delete any files with the name 'Logo7111830555559804.png' inside the PBIX
    Start-SevenZip u $TempDestination "C:\Temp\Report" #Copy our C:\Temp\Report folder into the PBIX File
    Copy-Item -Path $TempDestination -Destination $file.FullName #Copy the PBIX back to its original location
    Remove-Item -Path $TempDestination #Delete that temp PBIX
    $updateStatus = "success" #log a success message
    }
    catch {
    $updateStatus = "failed" # If we get any errors, catch and log a failed message
    }
    $Result += [PSCustomObject]@{ #create the result object and pass the name of the PBIX and the status
        File = $file.Name
        Status = $updateStatus
        }
}
$Result # Print the result object

Its worth noting, if you want to only find PBIX files with a specific name, you can add a -Filter item to the $FoundFiles line, see below.

$FoundFiles = Get-ChildItem $SearchPath -recurse -Include *.pbix -Filter "*Example Report*" # This filter finds any pbix files with Example Report in the name

There we have it, the images in each PBIX file are now updated, and the result object will print, looking something like below (i forced an error by leaving the PBIX file open).

PowershellResults

To Do

Using the New-PowerBIReport cmdlet you could actually go ahead and write a line to publish the report using the powershell script. Something like the below at the end of the Try operation -

New-PowerBIReport -Path $file.FullName -WorkspaceId $worksapceid -ConflictAction CreateOrOverwrite # you need to set a workspace ID, conflict action could be Ignore, Abort, Overwrite, CreateOrOverwrite

Thanks for reading. Any questions - Twitter, LinkedIn or Email