top of page

Upload And Set Custom Master Page To Site Using CSOM PowerShell On SharePoint

In this article post, you will learn how to upload custom master page to master page gallery and apply custom master pages to the site using CSOM Powershell on SharePoint 2013 / SharePoint 2016 / SharePoint online sites.

Steps Involved:

The following prerequisites need to be executed before going for any operations using CSOM PowerShell on SharePoint sites.

  • Add the references using the Add-Type command with the necessary reference paths. The necessary references are Microsoft.SharePoint.Client.dll, Microsoft.SharePoint.Client.Runtime.dll.

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

  • Initialize client context object with the site URL.

$siteURL = ""

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)  

  • If you are trying to access SharePoint Online site then you need to setup the site credentials with credentials parameter and load it to the client context. 

# Not required for on premise site - Start

$userId = ""

$pwd = Read-Host -Prompt "Enter password" -AsSecureString  

$creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userId, $pwd)  

$ctx.credentials = $creds  

# Not required for on premise site - End

  • If you are trying to access the SharePoint on premise site then the credentials parameter is not required to be set to the context but you need to run the code on the respective SharePoint Server or you need to pass the network credentials and set the context.

# Credentials for on premise site - Start

$pwd = Read-Host -Prompt "Enter password" -AsSecureString  

$creds = New-Object System.Net.NetworkCredential("domain\userid", $pwd)  

$ctx.Credentials = $creds  

# Credentials for on premise site - End

Upload and Apply Master Page:

  • We will see how we can upload the master page file from local system to master page gallery and apply the uploaded master page to the site. Get the master page from local.

  1. Get the master page file local system URL and access the properties of master page file using Get-ChildItem method.

  2. Then read the master page content using ReadAllBytes method and initialize the content to variable ($fileBytes).

$pageFilePath = "D:\custom.master"

$pageFilePath = Get-ChildItem $pageFilePath  

$fileBytes = [System.IO.File]::ReadAllBytes($pageFilePath.FullName)  

  • Set the master page file details.

  1. Initialize file creation information object to set the master page file properties to be uploaded. 

  2. To the object created, set the necessary parameters like URL, content. Overwrite parameter can be used to force write the file to the server.

$newFileInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation  

$newFileInfo.Url = $pageFilePath. Name  

$newFileInfo.Content = $fileBytes  

$newFileInfo.Overwrite = $true

  • Access the folder and add the master page.

  1. Using the context, access the web. It should be root web since master page gallery is present at the top level site.

  2. Then access the master page gallery folder using web and GetByTitle method.

  3. From the web, get the source list and destination list using the list names. Then access the appropriate folder and the files method to add our new master page. Add new file creation information object created above. Load and execute the query.

$web = $ctx.Site.RootWeb  

$folder = $web.Lists.GetByTitle("Master Page Gallery")  

$masterPage = $folder.RootFolder.Files.Add($newFileInfo)  

$ctx.Load($web)  $ctx.Load($masterPage)  


  • Publish as major version.

  1. Then to publish the master page, we need to check out the file. Check out is required since the master page is neither checked out nor checked in as major version when the master page is uploaded. Load and execute the query for check out operation. 

  2. Then check in the master page with comments and check in type. Check in type should be major version.

# Check Out $masterPage.CheckOut()  



# Check In and Publish $masterPage.CheckIn("Majorversion checkin",[Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)  



  • We have successfully uploaded the master page. No we will apply the master page to the site. 

  1. From the web object, access the custom master url property and set the master page relative url. 

  2. To set master page for custom pages, use CustomMasterUrl property. 

  3. To set master page for system master pages, use MasterUrl property. 

  4. After setting the properties, update the web object. Then load and execute the web object with the context.

$web.CustomMasterUrl = $masterPage.ServerRelativeUrl  

$web.MasterUrl = $masterPage.ServerRelativeUrl  




The above steps will help us in uploading and setting the master page. The article focused on setting master pages for root level sites. If the master page needs to be set for subsite, then corresponding web object needs to be created before applying master page.



bottom of page