How to automate applying a SharePoint theme across site collections with PnP PowerShell

To enhance the user experience and brand identity of your SharePoint sites, you may want to apply a consistent theme across all your site collections.

However, manually applying a theme to each site collection can be a time-consuming and tedious task. The good news is that you can easily automate this process using PnP PowerShell. In this blog post, we’ll walk you through the steps to apply a SharePoint theme to all site collections without manual intervention.

Apply a SharePoint theme accross site collections

In order to accomplish the steps described in this article you must install the latest version of the PnP PowerShell module, more information about how to do it can be found here.

  1. Copy the following script and update the $tenantName, $themeName with your own values
    $tenantName = "handsonsp"
    $themeName = "Explorer"
    $modernOnly = $True
    	$connection = Connect-PnPOnline -Url ("https://{0}" -f $tenantName) -Interactive -ReturnConnection
    } catch {
    	Write-Host "Unable to connect."
    	$Sites = (Get-PnPTenantSite -Template 'GROUP#0' -Connection $connection) + (Get-PnPTenantSite -Template 'SITEPAGEPUBLISHING#0'  -Connection $connection)
    	$Sites = Get-PnPTenantSite -Connection $connection
    Write-Host ("Applying the {0} theme" -f $themeName) -BackgroundColor Gray -ForegroundColor Black 
    foreach ($Site in $Sites){		
    		Write-Host -NoNewline ("* {0} - {1} - " -f $Site.Title, $Site.Url)
    		Connect-PnPOnline -Url $Site.Url -Interactive
    		Set-PnPWebTheme -Theme $themeName -WebUrl $Site.Url
    		Write-Host "Done" -BackgroundColor Green
    	} catch {
    		Write-Host "Fail" -BackgroundColor Red
    Write-Host "All Done"
  2. Authenticate and wait for the process to complete, as the theme is applied to the site collections you can see a report of the sites indicating if it was successfully applied
    Apply a SharePoint theme accross site collections

Even authenticating as a global administrator you will not get access to the sites where you don’t belong, on those ones the theme will not be applied.

No comments yet

Leave a Reply

I've been working with Microsoft Technologies over the last ten years, mainly focused on creating collaboration and productivity solutions that drive the adoption of Microsoft Modern Workplace.

%d bloggers like this: