Powershell Script to Change Page Layout on Publishing Pages


This is an useful script to change the Page Layout for all the publishing pages for a site.

$spWeb = Get-SPWeb("http://yourwebapplication.com/yoursite")
$pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($spWeb)
$pSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($spWeb.Site);

$siteLayouts = $pSite.GetPageLayouts($false)
$myLayout = $siteLayouts["/_catalogs/masterpage/yourpagelayout.aspx"]
#$myLayout
$query = New-Object Microsoft.SharePoint.SPQuery
$query.ViewAttributes = "Scope='RecursiveAll'"

$pages = $pWeb.GetPublishingPages($query)

foreach ($page in $pages)
{
	if ($page.ContentType.Name -eq "Folder")
	{
		continue
	}

	$page.Layout = $myLayout
	$page.update()
	$page.ListItem.File.Publish("")
	$page.ListItem.File.Approve("")
}
$spWeb.Dispose()

Get the script file here.

Have fun!!!

See you,

Amadeu.

2 Responses to Powershell Script to Change Page Layout on Publishing Pages

  1. Hi,

    I am facing lots of errors in executing this script. Beginning with the term Get -SPWeb is not recognized as the name of command. Can you please help me with this?

    • ascampanelli says:

      The Get-SPWeb Powershell cmdlet is defined on the SharePoint Powershell modules.
      You can include it using two methods:

      -Opening the SharePoint 2010 Management Shell on the start menu on your SharePoint box. This is the simplest way since it will load all the modules for your.

      -Run the following command on any powershell command window:
      if ((Get-PSSnapin “Microsoft.SharePoint.PowerShell” -ErrorAction SilentlyContinue) -eq $null)
      {
      Add-PSSnapin “Microsoft.SharePoint.PowerShell”
      }

      After your have a powershell window with the SharePoint modules loaded you can execute the code provided in the post.

      Please check the details on the Get-SPWeb cmdlet at http://technet.microsoft.com/en-us/library/ff607807.aspx.

Leave a comment