Updating Page Layout for all pages in Pages Library

Table of Contents

This is a common requirement where a new Page Layout is created to replace an existing out of the box Page Layout. Rather than having to touch all the pages you can simply run this function:

Function Update-SPPagesPageLayout ([string] $pageLayoutName, [string] $siteUrl, [string] $webUrl)
{
    $spsite = Get-SPSite -Identity $siteUrl;
    $web = $spsite.OpenWeb($webUrl);

    if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web) -eq $true)
    {
        $pubsite = new-object Microsoft.SharePoint.Publishing.PublishingSite($spsite);
        $pubweb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web);
        $pageLayouts = $pubsite.GetPageLayouts($true);
                      
        $pageLayouts | ForEach-Object {
            if ($_.Title -eq $pageLayoutName)
            {
                Write-Host "Found $pageLayoutName page layout"
                $pageLayout = $_;
            }
        }
        $pages = $pubWeb.GetPublishingPages();
        $pages | ForEach-Object {
            $page = $_;
            Write-Host $page.Title;
            $page.CheckOut();  
            $page.Layout = $pageLayout;
            $page.ListItem.Update(); 
            $page.CheckIn("Publishing PageLayout correction");
            if ($page.ListItem.ParentList.EnableModeration) 
            {  
                $page.ListItem.File.Approve("Publishing Page Layout correction"); 
            }
        }
        
    }
    $web.Close();
    $spsite.Close();
}

Update-SPPagesPageLayout "Basic Page" "http://www.sharepoint.com/sites/devwiki/" "dev/SharePoint 2007 Development Wiki"
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.

Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Hosted generously by CustomWare