How to Set the Master Page in SharePoint 2013 using PowerShell

Sathish Nadarajan
 
Solution Architect
December 5, 2015
 
Rate this article
 
Views
12997

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 + "ConfigXMLConfiguration.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.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment