How to Delete a Master Page in SharePoint using PowerShell?

After deploying a new master page as part of the new branding, We wanted to delete the old master page from all SharePoint sites. As we’ve applied the new master page, we don’t want our existing custom master page to appear in the master page gallery and master page selection page.

Delete a master page in SharePoint 2013 using PowerShell:

PowerShell script to delete the SharePoint master page.

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Site URL and Master page Name variables

#Get Objects
$web = Get-SPWeb $WebURL
$MasterPage = $web.GetFolder("_catalogs/masterpage").Files[$MasterPageName]

#Delete the Master page using PowerShell

write-host "Master page deleted!" 

This PowerShell script just deletes the given master page from the given web. Loop through each web and delete the master page from all.

Remember, If the master page you are trying to delete is configured as either Default master page or custom master page, You can’t delete it! Change the master page in your code first, and then try deleting it again.

Related post: Change Master Page in SharePoint using PowerShell

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *