Apply Theme To A Classic SharePoint Online Site From An Existing Site Using SharePoint PnP PowerShell

In this blog, I will demonstrate how to get the theme of the Classic SharePoint Online site and apply the same theme to another Classic SharePoint Online Site using SharePoint PnP PowerShell script.
Pre-requisites (Environment Details)
  • Windows PowerShell
  • SharePointPnpPowerShellOnline Module
Please install the SharePointPnpPowerShellOnline module if it’s not already present using the below command.
  1. Install-Module SharePointPnPPowerShellOnline  
Source SharePoint Classic site with Red Theme
Destination SharePoint Classic site with Blue Theme
Variables Explanations in this Articles
  • $O365ServiceAccount=""# Your Service Account Name
  • $O365ServiceAccountPwd="Test123$"#Your Service Account Password
  • $SourceSharePointSiteURL="" # Change Source SharePoint Site URL
  • $DestinationSharePointSiteURL="" # Change Destination SharePoint Site URL
Here you can see we have provided the password in plain text which you need to provide if you want this PowerShell script to run automatically through Timer Job or Scheduler .
For Manual Execution Please use Get-Credential command to read the User name and Password from the user input
Get Theme from Source SharePoint classic site
We have to get the theme of the Source SharePoint Classic site using the Get-PnPTheme command of PowerShell as a code snippet below.
  1. #Connecting to Source Site to get the current theme    
  2. Connect-PnPOnline -Url $SourceSharePointSiteURL -Credentials $PSCredentials    
  3. #Getting the theme of the site    
  4. $Theme=Get-PnPTheme -WarningAction Ignore    
  5. Disconnect-PnPOnline    
  6. #Disconnect source site   
First we have to connect the Source site using Connect-PnPOnline then get the theme of the site and store in the variable $Theme. Once we have stored the theme in the variable disconnect the site using Disconnect-PnPOnline command.
Set Theme to Destination SharePoint classic site
We have to set the theme to the Destination SharePoint classic site using the Set-PnPTheme as a code snippet below
  1. #Connecting to Destination site to Apply theme  
  2. Connect - PnPOnline - Url $DestinationSharePointSiteURL - Credentials $PSCredentials  
  3. if ($Theme.Theme) {  
  4.     #Apply the theme to the site  
  5.     Set - PnPTheme - ColorPaletteUrl $Theme.Theme - ErrorAction SilentlyContinue  
  6. }  
  7. if ($Theme.ColorFile) {  
  8.     #Apply the theme to the site  
  9.     Set - PnPTheme - ColorPaletteUrl $Theme.ColorFile - ErrorAction SilentlyContinue  
  10. }  
  11. Disconnect - PnPOnline  
  12. #Disconnect destination site  
The first step is to connect to the destination SharePoint classic site . if $Theme variable has ColorFile or Theme property, set the theme using Set-PnPTheme command.
In some sites I have observed $Theme.Theme is null so in that case we have to use $Theme.ColorFile as ColorPaletteUrl to set the theme of SharePoint Classic Site.
Complete PowerShell Script
  1. #Powershell script to Get a Theme of a site and set the same theme to another SharePoint Online Classic site.  
  2. #Created By: Vinit Kumar  
  3. # Variable - Change the parameter as it need  
  4. $O365ServiceAccount = ""  
  5. $O365ServiceAccountPwd = "Test123$"  
  6. $SourceSharePointSiteURL = ""  
  7. # Change Source SharePoint Site URL  
  8. $DestinationSharePointSiteURL = ""  
  9. # Change Destination SharePoint Site URL  
  10. #Ends  
  11. # Get credentials to connect sharepoint online[SecureString] $SecurePass = ConvertTo - SecureString $O365ServiceAccountPwd - AsPlainText - Force[System.Management.Automation.PSCredential] $PSCredentials = New - Object System.Management.Automation.PSCredential($O365ServiceAccount, $SecurePass)  
  12. #Ends  
  13. #Connecting to Source Site to get the current theme  
  14. Connect - PnPOnline - Url $SourceSharePointSiteURL - Credentials $PSCredentials  
  15. #Getting the theme of the site  
  16. $Theme = Get - PnPTheme - WarningAction Ignore  
  17. Disconnect - PnPOnline  
  18. #Disconnect source site  
  19. #Connecting to Destination site to Apply theme  
  20. Connect - PnPOnline - Url $DestinationSharePointSiteURL - Credentials $PSCredentials  
  21. if ($Theme.Theme) {  
  22.     #Apply the theme to the site  
  23.     Set - PnPTheme - ColorPaletteUrl $Theme.Theme - ErrorAction SilentlyContinue  
  24. }  
  25. if ($Theme.ColorFile) {  
  26.     #Apply the theme to the site  
  27.     Set - PnPTheme - ColorPaletteUrl $Theme.ColorFile - ErrorAction SilentlyContinue  
  28. }  
  29. Disconnect - PnPOnline  
  30. #Disconnect destination site  
Destination SharePoint Classic site with Red Theme  after Executing the Script
In this blog, we have talked about how to get a color theme of Source SharePoint Online Classic site and apply to a destination SharePoint online Classic site using SharePoint PnP PowerShell. This blog will be useful in scenarios where we need to create a new SharePoint site and apply an existing Corporate Site theme using PowerShell.