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

%3CLINGO-SUB%20id%3D%22lingo-sub-3208700%22%20slang%3D%22en-US%22%3ESharePoint%20-%20PowerShell%20script%20to%20force%20Modern%20Theme%20for%20all%20existing%20subsites%20in%20a%20Site%20Collection%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3208700%22%20slang%3D%22en-US%22%3E%3CP%3EIn%20SharePoint%20sites%2C%20we%20can%20a%20lot%20of%20subsites%20and%20assign%20the%20Modern%20Theme%20for%20each%20could%20be%20long.%3C%2FP%3E%3CP%3ETo%20simplify%20that%20action%2C%20I%20created%20the%20following%20PowerShell%20script%20to%20simplify%20this%20update.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou%20will%20need%20an%20access%20as%20O365%20SPO%20tenant%20admin%20to%20execute%20the%20command%20%22Set-PnPWebTheme%22%20as%20explained%20into%20the%20official%20documentation%3A%3C%2FP%3E%3CUL%3E%3CLI%3E%3CSTRONG%3E%3CA%20title%3D%22Set-PnPWebTheme%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fsharepoint-pnp%2Fset-pnpwebtheme%3Fview%3Dsharepoint-ps%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESet-PnPWebTheme%3C%2FA%3E%26nbsp%3B%3C%2FSTRONG%3E%3C%2FLI%3E%3C%2FUL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23Register-PSRepository%20-Default%20%23%26gt%3B%20Required%20to%20force%20PowerShell%20module%20reconfig%20when%20install-module%20command%20is%20failing%0A%23Install-Module%20-Name%20PnP.PowerShell%20-Force%20%23%26gt%3B%20Required%20to%20install%20PNP.PowerShell%20module%20on%20your%20computer%0A%0A%5Bstring%5D%24TeamSiteToUpdate%20%3D%20%22https%3A%2F%2FYourTenant.sharepoint.com%2Fsites%2FYourTeamsite%2F%22%0A%5Bstring%5D%24TenantRootURL%20%3D%20%22https%3A%2F%2FYourTenant.sharepoint.com%22%0A%5Bstring%5D%24SubsiteFullURL%20%3D%20%22%22%0A%5Bstring%5D%24ThemeName%20%3D%20%22Your%20Theme%20Name%22%0A%0AImport-Module%20PnP.PowerShell%20-DisableNameChecking%0A%0A%23Connect%20the%20root%20teamsite%20to%20get%20the%20list%20of%20SubSites%0AConnect-PnPOnline%20-Url%20%24TeamSiteToUpdate%20-UseWebLogin%0A%24web%20%3D%20Get-PnpWeb%0A%24AllSubWebs%20%3D%20Get-PnPSubWeb%20-Recurse%0ADisconnect-PnPOnline%0A%0A%23Connect%20the%20SharePoint%20Tenant%20to%20force%20the%20defined%20Theme%20into%20each%20of%20the%20subsites%20and%20root%20site%0AConnect-PnPOnline%20-Url%20https%3A%2F%2FYourTenant-admin.sharepoint.com%20%20-UseWebLogin%0ASet-PnPWebTheme%20-Theme%20%24ThemeName%20%E2%80%93WebUrl%20%24TeamSiteToUpdate%0A%0A%0Aforeach(%24MySubsite%20in%20%20%24AllSubWebs)%0A%7B%0A%20%20%20%20%24SubsiteFullURL%20%3D%20%24TenantRootURL%20%2B%20%24MySubsite.ServerRelativeUrl%0A%20%20%20%20Write-Host%20%22Subsite%20Name%3A%22%2C%20%24MySubsite.Title%2C%20%22%20-%20Full%20URL%3A%22%2C%20%24SubsiteFullURL%20%2C%22(RelativeURL%3A%22%2C%20%24MySubsite.ServerRelativeUrl%2C%20%22)%22%0A%20%20%20%20Set-PnPWebTheme%20-Theme%20%24ThemeName%20%E2%80%93WebUrl%20%24SubsiteFullURL%0A%20%20%20%20Write-Host%20%22%20%26gt%3B%26gt%3B%26gt%3B%20Theme%20Applied%20-%20%22%2C%20%24ThemeName%20-ForegroundColor%20Green%0A%7D%0A%0ADisconnect-PnPOnline%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20your%20script%20is%20adapted%20and%20executed%2C%20you%20will%20find%20the%20root%20site%20and%20subsites%20with%20the%20correct%20Theme.%20You%20can%20also%20use%20this%20script%20to%20adapt%20the%20case%20with%20different%20Theme%20for%20subsites%20versus%20rootsite%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSTRONG%3EFabrice%20Romelard%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3208700%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAdmin%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESites%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ETheme%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Regular 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