Compartilhar via


PowerShell: How to Report on All Web Templates Used In All Webs

This blog post describes how to generate a report of all web templates used in all webs.  The sample script I'm providing is scoped to an individual web application, however this could easily be expanded in scope to include all web applications.

Background

I was recently going through an upgrade from Microsoft Office SharePoint Server 2007  to SharePoint Server 2010.  The environment being upgraded had a complicated history and to make a long story short, nobody really knew what site templates were actually being used in the environment anymore.  There are obviously some tools that will help identify which site templates are being used in the environment.  Those include the pre-upgrade checker, and test-spcontentdatabase.  These tools really didn't give us the level of detail which we needed.  We needed something a little more granular in order to help clean up some of the templates we really didn't want to take into the SharePoint 2010 environment.

Approach

The approach we took really wasn't anything groundbreaking, but for what we wanted to do it was really helpful.  What we wanted to do was remove sites which were using templates we didn't plan on migrating to SharePoint 2010.  To do this, we installed all of the components, including all site templates, we wished to retain in SharePoint 2010.  After having done this, we attached a relatively clean content database to a web application  in the SharePoint 2010 environment.  Obviously since we were missing some templates, the upgrade finished with some errors and warnings - this was expected.  Next, we ran the script provided in order to identify sites which were using templates which were not present in the SharePoint 2010 environment.  We can actually cross-reference the output from the sample script provided with the ouput from the upgrade error log generated while attaching the SharePoint Sever 2007 database to the SharePoint Server 2010 environment.  We can match the TemplateID from both logs in order to delete the sites.

Solution

Just because the upgrade finished with errors and warnings, it doesn't mean that the effort was wasted.  Most site collections have been upgraded, all site collections can still be reported on.  Some of the sites which could not be upgraded due to their template can even still be accessed using a web browser.  As great as that is, we're not particularly interested in that right now.  We're really just interested in cleaning up the environment and removing references to site templates we no longer use.  This can be retrieved from each SPWeb object using two properties.  WebTemplate will return the display name of the site template and WebTemplateID will return the ID of the site template.  Now all we have to do is loop through all site collections in the web applicaiton, then all webs in each site collection and retrieve the data.  This is exactly what the sample script does.

The important part of the script looks something like this:

"Web URL: " + $Web.url | Out-File $LogFile -Append
"Web Template ID: " + $Web.WebTemplateID | Out-File $LogFile -Append
"Web Template: " + $Web.WebTemplate | Out-File $LogFile -Append

This is happening inside of nestef for loops, as described above.

Once we cleaned up the database using the new information gathered with this script, we ran upgrade-spcontentdatabase against the content database.  This time it came back cleanly, as expected.

You can download the Web Template Report script from this location: Download ListAllWebTemplates.ps1 (zipped)

Usage

The script provided has only two variables that must be configured.  The first one being the Web Application URL, and the second one being the Logging Directory.  Simply configure these two variables and run the PowerShell script.

Feedback

As always, if you have any questions or feedback, let me know. If you have any ideas to optimize the script, I'd like to hear that too. Thanks for reading!