How to Set the Master Page in SharePoint 2013 using PowerShell


Sathish Nadarajan
SharePoint MVP
Published On :   05 Dec 2015
Visit Count
Today :  4    Total :   6102
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


A small code snippet to set the Master Page in SharePoint 2013 using PowerShell. The Script is straight forward.

 ##================================================================================================
 ## Description	: To do the below two items.
     #1. Change the Master Page to the Custom Master Page
 ## Author		: Sathish Nadarajan
 ## Date			: 17-Nov-2015
 ##================================================================================================
 
 ##============================================ Setup Input Paths ===========================================================
 
 cls
  
 $Host.UI.RawUI.WindowTitle = "-- Change the Master Page to the Custom Master Page --"
 
 $StartDate = Get-Date
 Write-Host -ForegroundColor White "------------------------------------"
 Write-Host -ForegroundColor White "| Change the Master Page to the Custom Master Page |"
 Write-Host -ForegroundColor White "| Started on: $StartDate |"
 Write-Host -ForegroundColor White "------------------------------------"
 
 $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
 $LogFile = ".\ChangeMasterPage-$LogTime.rtf"
 
 #start-transcript $logfile
 
 $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
 Set-Location $scriptBase
 
 $ErrorActionPreference = "Stop"
 
 function AddPowerShellSnapin()
 {
     try
     {
         Write-Host "Adding PowerShell Snap-in" -ForegroundColor Green
         # Try to get the PowerShell Snappin.  If not, then adding the PowerShell snappin on the Catch Block
         Get-PSSnapin "Microsoft.SharePoint.PowerShell" 
     }
     catch
     {
         if($Error[0].Exception.Message.Contains("No Windows PowerShell snap-ins matching the pattern 'Microsoft.SharePoint.PowerShell' were found"))
         {
             Add-PSSnapin "Microsoft.SharePoint.PowerShell"
         }
     }
     Write-Host "Finished Adding PowerShell Snap-in" -ForegroundColor Green
 }
 
 function UpdateMasterPage([string]$webURL)
 {
     $web = Get-SPWeb $webURL
 #Change the masterpage
     $web.CustomMasterUrl = "/services/GVI/_catalogs/masterpage/MyMaster.master"
 
 #Change the System Master Page
     $web.MasterUrl = "/services/GVI/_catalogs/masterpage/ MyMaster-System.master"
     $web.Update()
 }
 
 try
 {
     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")
     [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint.Administration")
         
     $ConfigXmlPath = $scriptBase + "\ConfigXML\Configuration.xml"
     Write-Host "Read the Config Values" -ForegroundColor Green 
     [Xml]$Config = Get-Content $ConfigXmlPath  
     
     AddPowerShellSnapin
     
     UpdateMasterPage $Config.Configuration.PublishingSite.URL  
      
 }
 catch
 {
     Write-Host "Custom Exception Happened on Main : " + $Error[0].Exception.Message -ForegroundColor Red  
 }
 

And the Config XML Will be like,

 <Configuration EnvironmentName="Dev/QA/UAT/PROD">
   <PublishingSite URL="http://MySiteCollectionURL" />
      
 </Configuration>
 

Happy Coding,

Sathish Nadarajan.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Protect Your SharePoint