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.

Advertisements