Create SharePoint Site (SPWeb) using Custom Template and PowerShell

Published: Updated: 2 min readPowerShell, SharePoint Server

I got a task to setup company’s hierarchy using our own custom created site template. My first priority is to achieve this task through PowerShell. Creating site collection or sub site through PowerShell is fairly a simple task, the command for creating sub site is

New-SPWeb http://sharepoint/subweb1 -Template “STS#0”

Ref: http://technet.microsoft.com/en-us/library/ff607579(v=office.15).aspx

I tried to replace the template name with my custom template’s name which I found out through the below script:

Add-PSSnapin Microsoft.Sharepoint.Powershell
$web = get-spweb https://sharepoint
$template = $web.GetAvailableWebTemplates(1033) | Where-Object {$_.Title -eq “<Custom Templete Title>”}
Write-Host $template.Name

Create SharePoint Site (SPWeb) using Custom Template and PowerShell

In the below script substitute the default template name with my custom template name but this didn’t work

New-SPWeb -Url “https://sharepoint/newsubsite” -Template “{D839E38A-F9DF-4B35-BDA7-A1E273C51EAB}#XXXX Template”

 Output:

Create SharePoint Site (SPWeb) using Custom Template and PowerShell

After looking into documentation the parameter it takes as Template is SPWebTemplatePipeBind which is optional. If someone doesn’t set the template, the site will be created but the template isn’t applied which will then prompt for template selection against its first visit. Finally I came up with the below script which worked like a charm.

 Add-PSSnapin Microsoft.Sharepoint.Powershell
$web = get-spweb https://sharepoint
$template = $web.GetAvailableWebTemplates(1033) | Where-Object {$_.Title -eq “<Custom Templete Title>”}
$newweb = New-SPWeb -Url “https://sharepoint/newsubsite” 
$newweb.ApplyWebTemplate($template.Name)
Share:
Muhammad Sami Ahmed

Muhammad Sami Ahmed

Muhammad Sami Ahmed is a SharePoint developer and solution architect with a background in ASP.NET, C# and SQL Server, focused on custom SharePoint solutions.

More posts by Muhammad Sami Ahmed

Leave a Reply

Your email address will not be published. Required fields are marked *