Powershell script for updating a publishing page Page Layout in a site collection

One of the page layout templates was changed in a SharePoint 2010 site collection that I was working on. There were already over 300 pages that were creating using the old Page Layout. Enter… Powershell! It took about two hours to write this script (with help from The Google) but it will probably save a day of work updating all those pages individually through the UI.

The script takes three parameters, the old or current page layout name, the new page layout name and an optional –all parameter which will traverse all the sites of a site collection. Enjoy.

Hey Jake
Thanks for the code. I have a similar issues I have to solve that I’m using your script as the base for. My issue is that I have multiple site collections that I need to move under one primary site collection as sub-sites, under SP 2010.

I can export the site collection and import as a sub-site, but the page layout link is broken. I copied the page layouts and master pages to the primary site collection run and try to run this code to update the link for that page layouts. I get an error stating it could not update the Layout (Null Exception).

If I detach the page layout from one of the pages using SharePoint Designer and rerun the code it works.

One little missing line on the function. To actually reflect the updates on existing content pages that are using the layout, you’ll need to call the Update() method for the page itself right after the ListItem one…

This worked very well for me. I had to change the approval line as suggested in another comment. One thing that I think it is missing is that it did not check if the pages are already checked out by someone. It gave me errors for those pages which were checked out by someone. It would be be nice to perform that check and report the checked out files.

Another thing I changed in the script was the output method. I changed the Write-Host to Write-Output so that I could capture the output to a file using a pipe for example .\PageLayoutUpdate.ps1 “Page Layout” “Page Layout New” 2>&1 > c:\temp\test.txt

Hi Chris, nice PS! I tried this but I could not figure out where I can put the name of the current pagelayot and the new of the new one I would like to apply to let say http://MyIntra/NewsSite/. I can change the variable under settings for the site, but how can I specify the name of the PageLayouts?
One more question, what about to if you dont want to change the ‘home page’ for the site, let say I have 300 pages in the site, and I want to apply a new pagelayout for 299 pages and not the start page like http://MyIntra/NewsSite/Pages/Default.aspx mayby you can extend your script with this options?
Thanks!

I’m trying to use this script but I don’t know Powershell well enough. I’m sure the following question is extremely basic.

Where/how do I specify the parameters? It seems like the function looks for certain parameters such as $pageLayoutNew. Where/how do I specify what exactly that page layout is? There must be some way to provide a link or a file name or something.

Hi Jeremy. I made the assumption that you would name your PowerShell script file “UpdateLayoutPages.ps1” That was not a great assuption. But, if you do then you’d load the script that way through your PowerShell command shell. So something like this… PS C:\PSScripts> .\UpdateLayoutPages.ps1 myOutOfDatePage.aspx SingleColumnLayout.aspx

I’m specifying the parameters on the command line here, but I could also do it in another PowerShell file (say UpdateTenPages.ps1) and call UpdateLayoutPages.ps1 10 times for 10 different pages. Does that help? It’s a good question and points out that I named my PS1 file UpdateLayoutPages.ps1, which I never mentioned. Thanks!

Hi Jake. Thank you for your quick response. I’m sure your instructions were fine for anyone with Powershell experience. Now that you’ve given an example it makes sense.

I’m able to run the script. It checks the parameters and I get a message about “checking if both pages layout exist in the site” and then a “Done” message. However none of the page layouts are actually changed.

Anything else you can think of that I should check? Anything else I haven’t specified? Maybe a permissions issue?

It looks to me like your script actually does check out, check in, and approve items.

If I’m following the code correctly, right after the “Checking if both page layouts exist in the site…” message it then should check if the parameter I’ve listed as the current page layout actually exists in the site collection. And after checking it I should see “Found CURRENT page layout: ” I don’t see that. It just skips down to “done”. I’ve entered incorrect page layouts but it still just skips down to “done”.

Do you know if there is a way to output to a log file? Maybe that could show me an error message that I’m not able to see otherwise?