Top Menu

Tuesday, April 2, 2013

PowerShell - Change Master Page

Scenario:
I had to write a quick PowerShell script to change the Master Page for a SharePoint 2013 site. Thought to share it with the world.

Solution:
cls;

[void][reflection.assembly]::Loadwithpartialname("Microsoft.SharePoint")

$SiteUrl = "http://mySite1919/sites/MasterTest";
$NewMasterPage = "/_catalogs/masterpage/MasterPage_custom.master";

$Site=[Microsoft.SharePoint.SPSite]($SiteUrl);
$SiteWeb = $Site.openweb();
$SiteRelativeUrl = $Site.ServerRelativeUrl.TrimEnd('/');

Write-Host "Site: " $SiteUrl;
Write-Host "Current MasterPage: " $SiteWeb.CustomMasterUrl;

#Site Master Page
$SiteWeb.CustomMasterUrl = $SiteRelativeUrl + $NewMasterPage;
#System Master Page
$SiteWeb.MasterUrl = $SiteRelativeUrl + $NewMasterPage;

$SiteWeb.Update();

Write-Host "New MasterPage: " $SiteWeb.CustomMasterUrl

$SiteWeb.Dispose();

No comments:

Post a Comment

Official SharePoint Documentation

I have recently contributed to the official SharePoint documentation for developement. Check it out here: https://docs.microsoft.com/en-us...