Create publishing pages with PowerShell in SharePoint 2010

For a client we created an extended PowerShell script to do an One-Click deployment of their intranet. This was awesome to create because it really saved huge amounts of time creating sites & site collections, adding and activating solutions and setting permissions, navigation and master pages. But we still had to add a piece of code to create pages in the Pages Library. After a quick search, I came across this blogpost of Brendan Newell. It helped me out greatly! It just missed 1 crucial element; setting the page title. So I added this to his script. They were minor changes, so all credits go to Brendan.

PowerShell Script; # Read in list of pages from XML [xml]$pagesXML = Get-Content “pages.xml" if ($pagesXML -eq $null) { return } # Get publishing web $site = New-Object Microsoft.SharePoint.SPSite($server1) $web = $site.rootweb $pWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web) # Loop through each page node to extract filename and titlename $pagesXML.Pages.Page | ForEach-Object { $fileName = [string]$_.FileName Write-Host “Creating $fileName” $titleName = [string]$_.TitleName Write-Host “Creating $titleName” # Create blank page $newPage = $pWeb.AddPublishingPage() $newPage.Update() # Update the filename to the one specified in the XML $newPage.ListItem["BaseName"] = $fileName $newPage.ListItem["Title"] = $titleName $newPage.ListItem.SystemUpdate() # Check-in and publish page $newPage.CheckIn(“”) $newPage.ListItem.File.Publish(“”); } # Dispose of the web $web.Dispose() XML Structure; <?xml version="1.0" encoding="utf-8"?> <Pages> <Page> <FileName>P1</FileName> <TitleName>Page One</TitleName> </Page> <Page> <FileName>P2</FileName> <TitleName>Page Two</TitleName> </Page> </Pages>