This script can be used to create new site collections based on the templates that are saved on the disk.
We have saved the existing team site as template. The template is saved into the disk. We want to create new site collection based on this template using PowerShell.
This script contains the following advanced function:
You can use this script in the following way:
Run the function. For example: CreateSiteCollection
Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.
#Check and add the necessary snapin
if ( (Get-PSSnapin -Name microsoft.sharepoint.powershell -ErrorAction SilentlyContinue) -eq $null )
{
Add-PsSnapin microsoft.sharepoint.powershell
}
# --------- The Function to create a site collection based on the custom web template
Function CreateSiteCollection
{
Try
{
$siteUrl = read-host -prompt "Please enter the url of the site collection you want to create"
Write-Host "`n"
$owner = read-host -prompt "Please enter the site collection Owner (domain\username)"
Write-Host "`n"
$SolutionPath = read-host -prompt "Please enter the full path for the solution (Web Template)"
Write-Host "`n"
$stringArray = $SolutionPath.Split('\');
$SolutionNewFileName = $stringArray[$stringArray.Length-1];
$iPos = $SolutionNewFileName.LastIndexOf('.')
$SolutionFileWithoutExtension = $SolutionNewFileName.Substring(0,$iPos)
$tempsite = New-SPSite $siteUrl -OwnerAlias $owner –Language 1033
Write-Host "Site created with blank template. Step 1 completed"
}
Catch
{
Write-Host "ERROR: Error occured while creating the Site collection at " $siteUrl " . Check the Exception details for more information on the error.`n" -foregroundcolor red -backgroundcolor black
$ErrorMessage = $_.Exception.Message;
Write-Host "Exception Message:" $ErrorMessage "`n" -foregroundcolor red -backgroundcolor black
return;
}
# Add the solution (saved template) to the site collection
$site = New-Object Microsoft.SharePoint.SPSite($siteUrl)
$solutions = $site.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::SolutionCatalog);
$solutionFile = $solutions.RootFolder.Files.Add($SolutionNewFileName, [System.IO.File]::ReadAllBytes($SolutionPath));
$solution = $site.Solutions.Add($solutionFile.Item.ID);
Write-Host "Solution uploaded and activated for the new site collection [" $site.Url "]. Step 2 completed"
$siteFeatures = $site.FeatureDefinitions;
$solutionId = $solution.SolutionId;
# Activate the feature (from saved template) to the site collection
foreach ($FeatureDefinition in $siteFeatures)
{
if ($FeatureDefinition.SolutionId.Equals($solutionId) -eq $true -And $FeatureDefinition.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Site)
{
$temp = $site.Features.Add($FeatureDefinition.Id,$false, [Microsoft.SharePoint.SPFeatureDefinitionScope]::Site);
Write-Host "Feature " $FeatureDefinition.Name " activated for the new site collection [" $site.Url "]. Step 3 completed"
}
}
#Apply the template to the web
$webTemplates = $site.RootWeb.GetAvailableWebTemplates(1033);
foreach($w in $webTemplates)
{
if($w.Title -eq $SolutionFileWithoutExtension)
{
$web = $site.RootWeb;
$web.ApplyWebTemplate($w);
$web.Update();
Write-Host "Web template " $w.Name " activated for the new site collection [" $site.Url "]. Step 4 completed"
}
}
$site.Close()
$site = $null;
Write-Host "Operation completed." ;
}
#Check and add the necessary snapin if ( (Get-PSSnapin -Name microsoft.sharepoint.powershell -ErrorAction SilentlyContinue) -eq $null ) { Add-PsSnapin microsoft.sharepoint.powershell } # --------- The Function to create a site collection based on the custom web template Function CreateSiteCollection { Try { $siteUrl = read-host -prompt "Please enter the url of the site collection you want to create" Write-Host "`n" $owner = read-host -prompt "Please enter the site collection Owner (domain\username)" Write-Host "`n" $SolutionPath = read-host -prompt "Please enter the full path for the solution (Web Template)" Write-Host "`n" $stringArray = $SolutionPath.Split('\'); $SolutionNewFileName = $stringArray[$stringArray.Length-1]; $iPos = $SolutionNewFileName.LastIndexOf('.') $SolutionFileWithoutExtension = $SolutionNewFileName.Substring(0,$iPos) $tempsite = New-SPSite $siteUrl -OwnerAlias $owner –Language 1033 Write-Host "Site created with blank template. Step 1 completed"} Catch { Write-Host "ERROR: Error occured while creating the Site collection at " $siteUrl " . Check the Exception details for more information on the error.`n" -foregroundcolor red -backgroundcolor black $ErrorMessage = $_.Exception.Message; Write-Host "Exception Message:" $ErrorMessage "`n" -foregroundcolor red -backgroundcolor black return; } # Add the solution (saved template) to the site collection $site = New-Object Microsoft.SharePoint.SPSite($siteUrl) $solutions = $site.GetCatalog([Microsoft.SharePoint.SPListTemplateType]::SolutionCatalog); $solutionFile = $solutions.RootFolder.Files.Add($SolutionNewFileName, [System.IO.File]::ReadAllBytes($SolutionPath)); $solution = $site.Solutions.Add($solutionFile.Item.ID); Write-Host "Solution uploaded and activated for the new site collection [" $site.Url "]. Step 2 completed" $siteFeatures = $site.FeatureDefinitions; $solutionId = $solution.SolutionId; # Activate the feature (from saved template) to the site collection foreach ($FeatureDefinition in $siteFeatures) {if ($FeatureDefinition.SolutionId.Equals($solutionId) -eq $true -And $FeatureDefinition.Scope -eq [Microsoft.SharePoint.SPFeatureScope]::Site) { $temp = $site.Features.Add($FeatureDefinition.Id,$false, [Microsoft.SharePoint.SPFeatureDefinitionScope]::Site); Write-Host "Feature " $FeatureDefinition.Name " activated for the new site collection [" $site.Url "]. Step 3 completed"}} #Apply the template to the web $webTemplates = $site.RootWeb.GetAvailableWebTemplates(1033); foreach($w in $webTemplates) {if($w.Title -eq $SolutionFileWithoutExtension) { $web = $site.RootWeb; $web.ApplyWebTemplate($w); $web.Update(); Write-Host "Web template " $w.Name " activated for the new site collection [" $site.Url "]. Step 4 completed"}} $site.Close() $site = $null; Write-Host "Operation completed." ; }
1. Assume we have saved a team site as template and then saved it on the disk.
2. Run the following command in SharePoint Management Shell:
Import-Module< filepath>\CreateSiteFromCustomWebTemplates.ps1
CreateSiteCollection
3. Provide the URL for new site collection when prompted.
4. Provide the Site collection owner when prompted (domain\username).
5. Provide the full path for the saved template when prompted.

Please Note:
The script assumes that the source site that was saved as template was a Team site. For other type of sites, we will have to modify this script to enable the dependency features as well.
Technical Resources
Web Templates
http://msdn.microsoft.com/en-us/library/aa979709(v=office.14).aspx
SPWeb.SaveAsTemplate Method
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.saveastemplate(v=office.14).aspx