Dot Net Mafia

Group site for developer blogs dealing with (usually) Ionic, .NET, SharePoint, Office 365, Mobile Development, and other Microsoft products, as well as some discussion of general programming related concepts.

A SharePoint MVP bringing you the latest time saving tips for Ionic, SharePoint, and Office 365.

How to: Use PowerShell to customize the theme of a SharePoint Modern Site

At Microsoft Ignite, they announced new capabilities for working with themes on modern sites. Although, the new theme picker they have shown has not rolled out, you can still make use of the new theming capabilities right now.

Here is what the old theme picker looks like. When we are done, we’ll have our own themes in there and the stock themes will be gone. No longer will your users pick some awful theme for your sites!

The easiest way to build your new theme is to go to the Theme Generator page on the Office Fabric site. Here you can set a color and it will automatically choose a palette for you. You can override the colors in the palette based upon your own company’s brand standard. Just pick a Primary theme color or enter in the hex or RGB codes.

When you scroll down, you will find the code you need, but scroll even further and you will see a preview of what the Fabric Palette looks like.

Once you are happy, we’ll cut and paste the theme definition into our PowerShell script. However, before we do that, we have to install the SharePoint Online Management Shell (if you don’t have it already) and connect to SharePoint Online. You’ll need version 16.0.6906.1200 or newer. If you haven’t updated in a while, download it again.

One you have it installed, you need to connect to SharePoint Online. You’ll need to use an account with the SharePoint administrator or Global Administrator roles. The process involves specifying the URL for your SharePoint Online tenant and the credentials.

If you have trouble connecting, you can find more details on Microsoft Docs. If you use multi-factor authentication, leave off the credential parameter and it will prompt you with an OAuth login prompt. I’ll usually save this snipped in a PowerShell script so I don’t have to look it up every time.

Now that you are connected, we can add a theme. Create a new PowerShell script using PowerShell ISE, Visual Studio Code, or whatever editor you like. Add the following to the script.

PowerShell expects the theme in the format of a dictionary object. That is what the helper method HashToDictionary does. Now we paste in our color palette from the Theme Generator into the call to the HashToDictionary method. Be sure to incldue the @ symbol. Here is what the palette looks like on the theme generator.

Cut and paste that into the script. The completed script will now look like the following:

The Add-SPOTheme Cmdlet has a few parameters. Name should be fairly self explanatory. Set IsInverted to true if you are using an inverted theme (ie high contract / black background). Add the Overwrite to allow you to update the theme after it has been created. Run the PowerShell script to see your new theme. Running it will not yield any output if it is successful.

Refresh your SharePoint site and click on Change the Look underneath the Cog Wheel. You should see your new theme.

Select the theme to apply it to preview it on your site.

Pretty simple right? Now, if you want to remove the out-of-the-box themes. That’s easy too. Go back to PowerShell and issue the following command.

Set-HideDefaultThemes $true

Refresh your site and you will see that the out-of-the-box themes are now gone.

That’s all there is to it. There are a few more commands if you want to inspect what themes are installed or you want to remove one. You can read more about them on Microsoft Docs. Give it a try today!