How to Set the Page Layout in SharePoint 2013 using PowerShell

Sathish Nadarajan
SharePoint MVP
Published On :   05 Dec 2015
Visit Count
Today :  1    Total :   9710

In this article, let us see how to change the PageLayout of a Publishing page using PowerShell in SharePoint 2013. All the PowerShell Scripts will be very much helpful and handy during the time of preparing the deployment package.

The Script is straight forward and does not require any explanation I guess.

 ## Description	: To do the below two items.
     #1. Update the PageLayouts of the Existing Pages
 ## Author		: Sathish Nadarajan
 ## Date			: 16-Nov-2015
 ##============================================ Setup Input Paths ===========================================================
 $Host.UI.RawUI.WindowTitle = "-- Update the Page Layouts of the Existing Pages --"
 $StartDate = Get-Date
 Write-Host -ForegroundColor White "------------------------------------"
 Write-Host -ForegroundColor White "| Update the Page Layouts of the Existing Pages |"
 Write-Host -ForegroundColor White "| Started on: $StartDate |"
 Write-Host -ForegroundColor White "------------------------------------"
 $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
 $LogFile = ".\UpdatePageLayout-$LogTime.rtf"
 #start-transcript $logfile
 $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
 Set-Location $scriptBase
 $ErrorActionPreference = "Stop"
 function AddPowerShellSnapin()
         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" 
         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 UpdatePageLayout([string]$WebURL, [string]$PageLayoutName,[string]$PublishingPageName)
     #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 $PageLayoutName}
     #Get Pages Library
     $PublishingPage = $PublishingWeb.GetPublishingPage($WebURL + "Pages/" + $PublishingPageName)  
     $PublishingPage.Layout = $NewPageLayout
     $PublishingPage.CheckIn("Page layout Updated via PowerShell")
     $PublishingPage.ListItem.File.Publish("Published via PowerShell")
     if ($PublishingPage.ListItem.ParentList.EnableModeration)
         $PublishingPage.ListItem.File.Approve("Publishing Page Layout Updated!");
     write-host "Updated Page layout on: "$PublishingPage.url -ForegroundColor Green
     $ConfigXmlPath = $scriptBase + "\ConfigXML\PageLayouts.Config.xml"
     Write-Host "Read the Config Values" -ForegroundColor Green 
     [Xml]$Config = Get-Content $ConfigXmlPath  
     foreach($Page in $Config.Configuration.Pages.Page)     
         UpdatePageLayout $Config.Configuration.PublishingSite.URL $Page.LayoutName $Page.Name
     Write-Host "Custom Exception Happened on Main : " + $Error[0].Exception.Message -ForegroundColor Red  

And the Config XML is as follows.

 <Configuration EnvironmentName="Dev">
   <PublishingSite URL="http://MySiteCollectionURL/" />
 	<Page Name="home.aspx" LayoutName=" Home-Layout.aspx"></Page>
 	<Page Name="books.aspx" LayoutName="Topic-Layout.aspx"></Page>

Happy Coding,

Sathish Nadarajan.