SharePoint OnPremises – PowerShell script to update master page of all webs in Web Application – required in migration

SharePoint On-Premises : PowerShell script to update master page
SharePoint On-Premises : PowerShell script to update master page

Hi All,

Greetings for the day 🙂 LIFE IS BEAUTIFUL 🙂

SharePoint migration continues 🙂 Today sharing small PowerShell script but very useful in migration

Background :

  • We are working on migration project. We are migrating from SharePoint 2010 to SharePoint 2016
  • First step in our migration is Migrating SharePoint 2010 web application to SharePoint 2013 since there is no direct migration available from SharePoint 2010 to SharePoint 2016
  • We did content db migrated and upgraded all site collections to 2013
  • Since we have around 100+ site collections, we did site collection upgrade through PowerShell script
  • While testing we found one issue, in upgrade our custom master page is replaced by OOB SharePoint 2013 seattle.master page and hence it was breaking since we have our custom page layout as well hence the current requirement 🙂
  • So we have very short PowerShell script to update custom master page for all our publishing pages but OOB application pages have OOB master page, we were not changing those
  • There are two properties of Web object for updating master page of the site – MasterUrl and CustomMasterUrl
  • SPWeb.MasterUrl => System master page. Master page for all forms and view pages in the site
  • SPWeb.CustomMasterUrl => Site master page. Master page for all publishing page
  • So just adding one more PowerShell script to our PowerShell script collection and sharing here. SHARING IS CARING 🙂

PowerShell script steps :

  • Add Microsoft SharePoint snap-in to the current session of PowerShell
Add-PsSnapin Microsoft.SharePoint.PowerShell 

  • Add CMDLET for disposing the objects used with variable assignment
Start-SPAssignment
     [-AssignmentCollection <SPAssignmentCollection>]
     [-Global]
     [<CommonParameters>]

Start-SPAssignment -Global

  • Here, if we use “Global” parameter with CMDLET Start-SPAssignment – all objects are assigned to the lifetime of the script and are disposed of when the Stop-SPAssignment cmdlet is called
  • Get the web application of which for all site collections we need to update the master page
$webApp = Get-SPWebApplication -Identity "<My WebApplication URL>"

  • Iterate through all site collections in web application
  • And then for every web of each site collection since we will use the “CustomMasterUrl” property of every web object as
foreach($spSite in $webApp.Sites){
  foreach ($web in $spSite.AllWebs){
      $web.CustomMasterUrl = $spSite.RootWeb.ServerRelativeUrl + "/_catalogs
                             /masterpage/knlwedgejunction.master";
     $web.Update();
    Write-Host $web.Url $web.MasterUrl $web.CustomMasterUrl;
  }#foreach ($web in $spSite.AllWebs)
}#foreach($spSite in $webApp.Sites)

Finally use the CMDLET – Stop-SPAssignment

Stop-SPAssignment
    [[-SemiGlobal] <SPAssignmentCollection>]
    [-AssignmentCollection <SPAssignmentCollection>]
    [-Global]
    [<CommonParameters>]

Stop-SPAssignment -global

Complete Script :

Add-PsSnapin Microsoft.SharePoint.PowerShell

Start-SPAssignment -Global

$webApp = Get-SPWebApplication -Identity "<My WebApplication URL>"

foreach($spSite in $webApp.Sites){
  foreach ($web in $spSite.AllWebs){
      $web.CustomMasterUrl = $spSite.RootWeb.ServerRelativeUrl + "/_catalogs
                             /masterpage/knlwedgejunction.master";
     $web.Update();
    Write-Host $web.Url $web.MasterUrl $web.CustomMasterUrl;
  }#foreach ($web in $spSite.AllWebs)
}#foreach($spSite in $webApp.Sites)

Stop-SPAssignment -global

References:

Thanks for reading 🙂 If its worth at least reading once, kindly please like and share 🙂 SHARING IS CARING 🙂

Enjoy the beautiful life 🙂 Have a FUN 🙂 HAVE A SAFE LIFE 🙂 TAKE CARE 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading