Use PowerShell to "Reset to Site Definition" in SharePoint Server 2010

In one of my posts last month, I provided the following steps to "reghost" all of the pages in a Team Foundation Server (TFS) project site:

  1. Browse to the Site Settings page for the site (e.g. https://cyclops/sites/Demo/_layouts/settings.aspx).
  2. On the Site Settings page, in the Site Actions section, click Reset to site definition.
  3. On the Reset Page to Site Definition Version page, click the option to Reset all pages in this site to site definition version, and then click Reset.

Today, I was about to perform this process manually on several sites, but then I decided to spend a few minutes exploring the Reset Page to Site Definition Version page (e.g. https://cyclops/sites/Demo/_layouts/reghost.aspx).

That's when I discovered the SPWeb.RevertAllDocumentContentStreams method. [I'm actually a little embarrassed that I wasn't aware of this little gem before. A few years ago, while working on the Agilent Technologies project, I'd used some custom C# to reghost specific pages (to resolve deployment issues), but I clearly recall some general "wonkiness" with it. If I'd known about the RevertAllDocumentContentStreams method a few years ago, I probably would have just run that as part of each and every deployment ;-) ]

Anyway, once you know about the method, it's very easy to call it from PowerShell.

Here's a little script to reset all pages in a list of sites to the site definition version:

 $sitesToReset =
    @(
        "https://cyclops/sites/AdventureWorks",
        "https://cyclops/sites/Demo",
        "https://cyclops/sites/Toolbox"
    )

$sitesToReset |
    ForEach-Object {
        $DebugPreference = "SilentlyContinue"
        $web = Get-SPWeb $_

        $DebugPreference = "Continue"
        
        Write-Debug "Reghosting all pages in site ($($web.Url))..."
        $web.RevertAllDocumentContentStreams()
        $web.Dispose()
    }