SharePoint - PowerShell script to force Modern Theme for all existing subsites in a Site Collection

Steel Contributor

In SharePoint sites, we can a lot of subsites and assign the Modern Theme for each could be long.

To simplify that action, I created the following PowerShell script to simplify this update.

 

You will need an access as O365 SPO tenant admin to execute the command "Set-PnPWebTheme" as explained into the official documentation:

 

#Register-PSRepository -Default #> Required to force PowerShell module reconfig when install-module command is failing
#Install-Module -Name PnP.PowerShell -Force #> Required to install PNP.PowerShell module on your computer

[string]$TeamSiteToUpdate = "https://YourTenant.sharepoint.com/sites/YourTeamsite/"
[string]$TenantRootURL = "https://YourTenant.sharepoint.com"
[string]$SubsiteFullURL = ""
[string]$ThemeName = "Your Theme Name"

Import-Module PnP.PowerShell -DisableNameChecking

#Connect the root teamsite to get the list of SubSites
Connect-PnPOnline -Url $TeamSiteToUpdate -UseWebLogin
$web = Get-PnpWeb
$AllSubWebs = Get-PnPSubWeb -Recurse
Disconnect-PnPOnline

#Connect the SharePoint Tenant to force the defined Theme into each of the subsites and root site
Connect-PnPOnline -Url https://YourTenant-admin.sharepoint.com  -UseWebLogin
Set-PnPWebTheme -Theme $ThemeName –WebUrl $TeamSiteToUpdate


foreach($MySubsite in  $AllSubWebs)
{
    $SubsiteFullURL = $TenantRootURL + $MySubsite.ServerRelativeUrl
    Write-Host "Subsite Name:", $MySubsite.Title, " - Full URL:", $SubsiteFullURL ,"(RelativeURL:", $MySubsite.ServerRelativeUrl, ")"
    Set-PnPWebTheme -Theme $ThemeName –WebUrl $SubsiteFullURL
    Write-Host " >>> Theme Applied - ", $ThemeName -ForegroundColor Green
}

Disconnect-PnPOnline

 

 

When your script is adapted and executed, you will find the root site and subsites with the correct Theme. You can also use this script to adapt the case with different Theme for subsites versus rootsite

 

Fabrice Romelard

 

0 Replies