SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using a PowerShell script to update page titles and PublishingPageLayout's in a particular web that has 391 pages. However, I'm finding that the longer the script runs, the slower each pages is updated, so I'm wondering how I can speed things up.

Here's my script (cut down for readability)

function ProcessSubWebs($currentWeb)
        $publishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($currentWeb)
        $publishingPages = $publishingWeb.GetPublishingPages()
        foreach ($publishingPage in $publishingPages)
            if($publishingPage.ListItem.File.CheckOutStatus -eq "None")
                UpdatePage -page $publishingPage

        foreach($sub in $currentWeb.Webs)
            if($sub.Webs.Count -gt 0)
        Write-Host -ForegroundColor red "FINISHED"
        Write-Host -Foregroundcolor Red "^ not a publishing site" 

function UpdatePage($page)
    Write-Host -Foregroundcolor green $page.Url;
    $NewPageTitle = $page.Title.Replace("Questionmark - ","");
    $page.Title = $NewPageTitle;

    $NewPageLayout = "/_catalogs/masterpage/QM_KB_NoCommentsRatings.aspx, QM KB Article No Comments or Ratings";
    $page.ListItem.Properties["PublishingPageLayout"] = $NewPageLayout;
    $page.ListItem.File.Approve("Updated PageLayout and Title");

ProcessSubWebs(Get-SPWeb -identity

I'm wondering if there is a way to 'close' or 'dispose' the page object? I'd tried just using .Close() and .Dispose() but they return errors. Is there another technique I can use to close each page after it's Approved, to make the script run faster?

I'm about to run the script on over 500 pages in multiple webs/sites.. but even if I find out afterwards, I think this would be useful info to know.

share|improve this question
There is no need to call Dispose method on Page because the page does not implement IDispose interface. – Ashish Patel Feb 11 '12 at 16:49
OK, that make sense. So the question is, why does my script get slower and slower the longer it run, and the more pages it approves, and is there anyway to speed it up, or at least keep it running as quickly as it does when I first execute it? – QMKevin Feb 11 '12 at 17:04
One thing you can try is Get SPSite using Get-SPSite and then use SPSite.AllWebs which gives all Webs recursive, so no need to write a recursive function. – Ashish Patel Feb 11 '12 at 17:19
I'll try that, but this code is meant to be for a specific site within the site collection, rather than all sites (or, to use PowerShell terms.. a single web and it's sub-webs rather than the entire site) – QMKevin Feb 11 '12 at 17:29
up vote 3 down vote accepted

You're not disposing $sub on each iteration. Do this outside your if test in the foreach.

share|improve this answer
Thanks - that kept the script running reasonably smoothly :) – QMKevin Feb 12 '12 at 1:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.