Get-SPWebTemplate Not Displaying Custom Web Templates

The other day I was trying to create a new sub site with PowerShell using a custom web template. The web template was being deployed using a sandboxed solution.

When creating a subsite, I usually use some PowerShell like the following:

New-SPWeb -Url "http://intranet/site1" -Name "Site 1" -Template "STS#0"

When I tried using this, passing in the custom web template Name, it resulted in a warning stating:

WARNING: Template is not found and is not applied.

After looking into the documentation for New-SPWeb, it showed that the Template parameter takes a SPWebTemplatePipeBind object. This led me to look at the documentation for Get-SPWebTemplate which states “Displays all globally installed site templates that match the given identity.” A sandboxed solution is not globally deployed, which explains why the template couldn’t be found.

After a bit of digging I found this blog post which applies custom web templates to subsites using PowerShell. They key bit in here is the method “GetAvailableWebTemplates(1033)”. This shows all web templates which are available on the current web. This means that it includes sandboxed solutions, where Get-SPWebTemplate does not:

To create a new subsite, based off of a custom web template, you can use the following script

$web = get-spweb http://intranet
$template = $web.GetAvailableWebTemplates(1033) | Where-Object {$_.Name -eq "My Custom Template Name"}
New-SPWeb -Url "http://intranet/site1" -Name "Site 1" –Template $template