SharePoint on-premises – PowerShell script to upload master page. Required in migration

SharePoint - Upload Master Page
SharePoint - Upload Master Page

Hi All,

Greetings for the day 🙂 LIFE IS BEAUTIFUL 🙂

Background : We were migrating our MOSS (SharePoint 2007) application to SharePoint 2010. We are using content database attachment method. Our DBs are mounted successfully to SharePoint 2010 web application. Next step is Visual upgrade. We are using following PowerShell CMDLET to visually upgrade (UI Version 4) all webs of respective site collection

#get the site which need to migrate
$site = Get-SPSite "<My SiteCollection URL>"
#get all webs of site collection - this will fetch all sub webs as well
$webs = $site.AllWebs
foreach($web in $webs){
   #check if site is still in MOSS - SP 2007 - if yes then update to version 4
   if($web.UIVersion -eq 3){
      $web.UIVersion = 4
   }#if($web.UIVersion -eq 3)
}#foreach($web in $webs)

While upgrading for some of the webs we are getting following error :

The Default master page “v4.master” not found

so we need to upload the default master page on respective webs. As usual PowerShell our friend 🙂

Following is the PowerShell to upload master page with detailed comments

#Source path of master page which we need to upload
$sourcepath = "D:\PS\default SP 2010 master page\v4.master"
#read our source master page
$MasterPageFile = (Get-ChildItem $sourcepath).OpenRead()

#Getting the site collection which we need to upgrade
$site = Get-SPSite "<My SiteCollection URL>"
#getting all webs of site collection
$webs = $site.AllWebs
#loop through all the webs of site collection to check if respective master page exists or not and if not then to upload it
foreach($web in $webs)
    #Respective web master page gallery
    $targetpath = $web.Url + "/_catalogs/masterpage/v4.master"

   #Check if respective master page file exist already
   if ($web.GetFile($targetpath).Exists){
   }#if ($web.GetFile($targetpath).Exists)

    #get the master page library of respective web
    $MasterPageList = $web.GetFolder("_catalogs/masterpage")
    #add our source master page file to respective web master page library 
    $MasterPage = $MasterPageList.Files.Add($targetpath,$masterPageFile,$false)
}#foreach($web in $webs)

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, 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.

%d bloggers like this: