View installed Site Definitions using PowerShell


A while ago I wrote about a little tool I’ve made that help you discover the ID’s of all Site Definitions installed on the server. SharePoint requires you to provide unique ID’s for custom Site Definitions and using a tool is definitely easier than manually browsing through all the WebTemp files and noting which ID’s are already used. But who wants a tool if you can do the same with PowerShell?

A brief glance in the past

The tool I wrote about back then was a custom STSADM command. While it integrated pretty nicely with SharePoint 2007 back then, STSADM is considered legacy now. Not surprisingly PowerShell is the way to go: you can do more with it than you can do with STSADM in SharePoint 2010, it is more flexible and the whole PowerShell engine is more powerful than the Windows Command Line.

Viewing installed Site Definitions with PowerShell

Viewing installed Site Definitions with PowerShell is very simple and can be done using the following snippet:

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint");

function Get-InstalledSiteDefinitions {
    param (
        [int] $lang = 1033
    )
    
    if ($lang -eq $NULL) { $lang = 1033; }
    
    $path = [Microsoft.SharePoint.Utilities.SPUtility]::GetGenericSetupPath("TEMPLATE\" + $lang + "\XML\WebTemp*.xml");
    if (Test-Path $path) {
        Select-String -Path $path -Pattern '(?:<Template[^>]+ID="(?<ID>[^"]+)"[^>]*>)' -AllMatches | ForEach-Object { [int]$_.Matches[0].Groups[1].Value } | Sort-Object
    }
}

Get-InstalledSiteDefinitions $args[0]

First the Microsoft.SharePoint.dll assembly is being loaded. While it’s not necessary when using the SharePoint 2010 Management Shell it’s required if you want to use this snippet in Windows PowerShell or with SharePoint 2007.

Then the Get-InstalledSiteDefinitions function is defined. The function allows you to pass the Locale ID as a parameter. Once the path where the WebTemp files for the given language are stored has been determined, the function will retrieve all ID’s from all the WebTemp files and will sort them ascending.

Here is the sample output when running the function on a SharePoint 2010 server:

List of ID’s of the installed Site Definitions produced by the Get-InstalledSiteDefinitions PowerShell function.

As you can see you can very easily view the list of ID’s of all installed Site Definitions and because it’s PowerShell you can easily extend the function to your specific needs.

Technorati Tags: SharePoint,SharePoint 2010,PowerShell

Others found also helpful: