Today I had to change the title of several pages in several publishing sites. You can find the powershell script below.
Notice that you have to access the pages as PublishingPage and not SPListItem,otherwise you will not be able to modify the Title field ("Title" is ReadOnly). Also, you have to go through the same procedure as in the UI: checkout, change property, checkin and publish (and maybe approve if the library requires it).
Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
#read list
$lines = Get-Content "C:\SharePoint2010\PowerShell\sites.txt" #| Sort-Object
foreach($l in $lines)
{
try
{
$a = $l.split(";");
$url = $a[0]
$title = $a[1]
$spWeb = Get-SPWeb -Identity $url
if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($spWeb))
{
$spPubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($spWeb)
$pages = $spPubWeb.PagesList
foreach($item in $pages.Items)
{
$pubPage = [Microsoft.SharePoint.Publishing.PublishingPage]::GetPublishingPage($item)
if ($pubPage.Title -eq "wrong title")
{
$pubPage.CheckOut()
$pubPage.Title = $title
$pubPage.Update();
$pubPage.CheckIn("")
$pageFile = $pubPage.ListItem.File;
$pageFile.Publish("");
#pageFile.Approve(checkInComment);
}
}
}
$spWeb.Dispose()
Write-Host -BackgroundColor DarkGreen -ForegroundColor White ("Done: " + $url);
}
catch
{
Write-Host -BackgroundColor DarkRed -ForegroundColor White ("Failed:" + $url + ". Error details : " + $_)
}
}
The input file contains the url of the site and the title of the page, separated by a semicolon. Like this:
http://<servername>/home/site1/site2;Correct Title for page
Technorati Tags: PowerShell, SharePoint