SharePoint Online: Apply Modern Theme using PowerShell

Requirement: Change the site theme in SharePoint Online.

How to apply theme in SharePoint Online?
In SharePoint Online, Themes are responsible for colors in various elements of the site. There are set of default site themes available for selection. You can also add your own custom theme to SharePoint Online. To set a theme in SharePoint Online modern sites,
  • Open your SharePoint Online site where you want to change the theme >> Click on the "Settings" gear icon.
  • In the "Settings" menu, click on "Change the Look"
  • Under themes, Select your desired theme (out of the box or custom theme) and click the "Save" button. The colors on themes can be further customized by clicking on the "Customize" button.
    sharepoint online change theme
  • This changes the theme for the site. Any user with a Design or Full Control permission level have the ability to change site themes.
Here is my other post on adding a modern theme using PowerShell in SharePoint Online: How to Add a Modern Theme using PowerShell in SharePoint Online?

PnP PowerShell to Change Theme in SharePoint Online
Here is how to change the theme in SharePoint Online using PowerShell. Make sure you have the theme added prior to running this script.
#Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$SiteURL = "https://creescent.sharepoint.com/sites/marketing"
$ThemeName = "Crescent Theme"
 
#Connect to Admin Center
Connect-PnPOnline -Url $AdminCenterURL -UseWebLogin
 
#Apply Theme to Web
Set-PnPWebTheme -Theme $ThemeName -WebUrl $SiteURL

PnP PowerShell to Apply Theme to All Subsites in a Site Collection 
You can change the look and feel of a SharePoint Online site to match your corporate color scheme through themes.
#Function to set Theme for a web
Function Set-PnPSiteTheme($Web, [String]$ThemeName)
{
    Try {
        Write-host "Applying Theme on site: " $Web.URL
 
        #Set Theme for the site
        Set-PnPWebTheme -Theme $ThemeName -Web $Web -Connection $AdminConn
 
        #Get All Subsites
        $SubWebs = Get-PnPSubWebs -web $Web -Connection $SiteConn
        Foreach ($Web in $SubWebs)
        {
            #Call the function recursively
            Set-PnPSiteTheme -Web $Web -ThemeName $ThemeName
        }
    }
    catch {
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
}
 
#Parameters
$AdminCenterURL = "https://crescent-admin.sharepoint.com"
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"
$ThemeName= "Corporate Theme"
  
#Connect to Admin Center
$AdminConn = Connect-PnPOnline -Url $AdminCenterURL -UseWebLogin -ReturnConnection

#Connect to SharePoint Online Site
$SiteConn = Connect-PnPOnline -Url $SiteURL -UseWebLogin -ReturnConnection

#Get the Root Web
$Web = Get-PnPWeb -Connection $SiteConn
 
#Call the function to set site theme for site collection
Set-PnPSiteTheme -Web $Web -ThemeName $ThemeName

How to Apply an OOTB Modern Theme in SharePoint Online using PowerShell
Alright, Instead of a custom theme, If you want to apply an out of the box default themes to your SharePoint Online sites, here is the script:
#Config Variables
$SiteURL = "https://crescent.sharepoint.com/sites/intranet"
  
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
$Web = Get-PnPWeb 

#Apply OOTB Modern Theme
[OfficeDevPnP.Core.Utilities.Themes.ThemeManager]::ApplyTheme($Web, [OfficeDevPnP.Core.Enums.SharePointTheme]::Blue)
Here is the enumeration of available themes:
powershell to change sharepoint online site theme

To apply a composite that includes a color theme, font, and background URL in SharePoint Online, use: SharePoint Online: How to Change Site Theme using PowerShell?

6 comments:

  1. pnpPowershell to set theme doesn't work.
    need to set Connect-PnPOnline url as -admin, not the site url you are applying theme to.

    ReplyDelete
    Replies
    1. Yes, We need Tenant Admin Rights to run "Set-PnPWebTheme" cmdlet and must connect to Tenant Admin!

      Delete
  2. Set-PnPSiteTheme function does not work and throw a 401 error.
    Is there a workaround for that ?
    With Connect-PnPOnline as -admin the $web will be admin site...
    Help please

    ReplyDelete
  3. This article saved me a lot of time. God bless you, Salaudeen.

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.