Apply SharePoint master page using PnP PowerShell

In this article, we learn how to apply SharePoint master page using PnP PowerShell to a particular site collection or its subsites. SharePoint has two master pages one for site pages and one for system pages. PnP PowerShell provides several commands which can be executed from a remote machine instead to run from the server.

Apply SharePoint master page using PnP PowerShell

Below is the PnP PowerShell command to apply the SharePoint master page using PnP PowerShell.

Set-PnPMasterPage
-MasterPageServerRelativeUrl <masterpagePath>
-CustomMasterPageServerRelativeUrl <masterpagePath>

Parameters Description –

MasterPageServerRelativeUrl –  This parameter takes the master page URL for all the system pages like settings.aspx and AllItems.aspx etc.

CustomMasterPageServerRelativeUrl – This parameter takes the master page URL for all the site pages like Home.aspx and default.aspx

 

Apply SharePoint master page using PnP PowerShell to Particular Site Collection

 

#Config Variables
$SiteURL =  "https://sp2016site.com/sites/project"
try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -UseWebLogin
    #Root Site Master Page URL
    $masterPageSiteRelativeUrl = "/_catalogs/masterpage/custom.master"
    #Apply Master page using PnP PowerShell
    Set-PnPMasterPage
    -MasterPageServerRelativeUrl $masterPageSiteRelativeUrl 
    -CustomMasterPageServerRelativeUrl $masterPageSiteRelativeUrl

}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}


Note – Make sure you change the “SiteURL” from your site collection URL before running the script.

This is how you can apply the SharePoint master page using PnP PowerShell to a particular site collection.

How to Apply SharePoint Master Page using PnP PowerShell to site Collection and its Sub Sites

Below is the example to apply the master page to site collection and its sub-sites using PnP PowerShell –

#Config Variables
$SiteURL = "https://sp2016site.com/sites/product"
try { 
#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -UseWebLogin
#Root Site collection Master Page URL
$masterPageSiteRelativeUrl = "/_catalogs/masterpage/custom.master"
#Get Root Site Collection and subsites in SharePoint Online using PnP PowerShell
$SubWebsCollections = Get-PnPSubWebs -Recurse -IncludeRootWeb
#Iterate through each subsite
ForEach($Web in $SubWebsCollections)
{
Connect-PnPOnline -Url $Web.Url -UseWebLogin
Set-PnPMasterPage -MasterPageServerRelativeUrl $masterPageSiteRelativeUrl -CustomMasterPageServerRelativeUrl $masterPageSiteRelativeUrl
}

}
catch {
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

First I make the connection to the Site Online site and then Iterate the root site collection and its sub-sites then one by one apply the top-level site collection master page to product site collection and its sub-sites.

You have noticed one thing in the above script I have used two below parameters with the PnPSubWebs command –

  1. -Recurse :  Default PnPSubWebs fetch only the immediate sub-site under the particular site collection but if we use -Recurse it fetch the immediate sub-site as well as all the nested subsites.
  2. -IncludeRootWeb : This parameter is used to get Rootsweb as well like in our case it gets product root web.

You can use the same code to get site collection and sub-sites just comment on the PnPMasterPage  PowerShell Cmdlet.

Conclusion

This is how you can apply the SharePoint master page using PnP PowerShell in SharePoint on-premises and SharePoint Online.

Leave a Reply

Your email address will not be published.