Change Page Layout in SharePoint using PowerShell

In a public internet SharePoint web site based on a publishing site template, We got a requirement to update existing page layouts and create new ones. So, the development team created new page layouts, Updated existing Page layouts with required changes. Any page going to be created with these updated page layouts will have the changes reflected. Well, How about existing pages created with the old page layouts? There is a requirement to switch page layouts for a few pages.

While it’s possible to change page layout through SharePoint web interface (Click on Edit Page >> Under “Page” tab, click on “Page Layout” and pick the different page layout you would like to set!), It would take hours to update the page layout of all existing pages. So, I wrote this script to change the page layout in SharePoint 2013 using PowerShell.

Change SharePoint page layout using PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables for Web and Page URLs
$WebURL="https://portal.crescent.com/sites/sales/"
$PageURL="https://portal.crescent.com/sites/sales/Pages/About-us.aspx"
$PageLayout="https://portal.crescent.com/_catalogs/masterpage/GlobalFunctionsPageLayout.aspx"

#Get the web and page
$Web = Get-SPWeb $WebURL
$File = $Web.GetFile($PageURL)

#change page layout sharepoint 2013 powershell 
$File.CheckOut("Online",$null)
$File.Properties["PublishingPageLayout"] = $PageLayout
$File.Update()
$File.CheckIn("Page layout updated via PowerShell",[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)

$Web.Dispose() 

In another case, During a site migration, I had to use this same method to change page layouts using PowerShell.
“This page is not using a valid page layout. To correct the problem edit page settings and select a valid page layout.” 

This page is not using a valid page layout. To correct the problem edit page settings and select a valid page layout

This is because Page layout URLs are hard-coded in publishing pages. When you move them from one site to another, We got to update it.

SharePoint 2016 change page layout using PowerShell:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables for Web and Page URLs
$WebURL="https://www.crescent.com/company-profile/"
$OldPageLayoutName="About-us.aspx"
$NewPageLayoutName="About-us-V2.aspx"

#Get the web and page
$Web = Get-SPWeb $WebURL

#Get Publishing Site and Web
$PublishingSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Web.Site)
$PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)

#Get New Page Layout
$SitePageLayouts = $PublishingSite.GetPageLayouts($false)
$NewPageLayout = $SitePageLayouts | ? {$_.Name -eq $NewPageLayoutName}

#Get Pages Library
$PublishingPages = $PublishingWeb.GetPublishingPages()

#Iterate throgh each page
foreach ($Page in $PublishingPages)
{
    if ($Page.Layout.Name -eq $OldPageLayoutName)
    {
        $Page.CheckOut()
        $Page.Layout = $NewPageLayout
        $Page.ListItem.Update();
        $Page.CheckIn("Page layout Updated via PowerShell")
    
        #$page.ListItem.File.Publish("")
        if ($Page.ListItem.ParentList.EnableModeration)
        {
            $Page.ListItem.File.Approve("Publishing Page Layout Updated!");
        }

     write-host "Updated Page layout on: "$Page.url
    }
}
$Web.Dispose()

The above script scans all publishing pages with an existing old page layout and changes page layout in SharePoint 2013 using PowerShell.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

3 thoughts on “Change Page Layout in SharePoint using PowerShell

  • Great. Thank you very much

    Reply
  • You are my man. Thank you very much for this. SharePoint 2013 ones worked perfectly fine for me without a single error.

    Reply

Leave a Reply

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