SharePoint 2010/2013: Powershell to create SharePoint Site Collection with custom Template
Save the custom site with all custom list\library etc as .wsp. This article walks you through the process:
1. Go to Site action > Site settings > Save site as template
http://itfreesupport.com/wp-content/uploads/2014/12/0103.png
Mention the name of template and select “Include content” > Click OK
http://itfreesupport.com/wp-content/uploads/2014/12/0104-300x226.png
Once the site is saved, you will get a link for “Solution gallery”. Go to Solution gallery and download the template (temp.wsp) file to something like d:\temp.wsp.
You need to write PowerShell in 2 parts. One will create site collection and the other will apply custom template.
1st Part of PowerShell
Start Part 1 Create a SharePoint Site Collection with a custom Template using Powershell
Write-Host “Provide the site collection URL e.g. http://intranet/site/custom”
$Siteurl = read-host
Write-Host “Provide the site collection owner email address”
$owner = read-host
New-SPSite $Siteurl -OwnerAlias $owner -Name “This will have a custom template”
End Part 1 Create a SharePoint Site Collection with a custom Template using Powershell
Now we have to add our WSP to the Solution Gallery in the Site Collection. I have a do While loop to make sure that the solution has been uploaded successfully before activating it.
##### Start Part 2 Create a SharePoint Site Collection with a custom Template using PowerShell ##############
Add-SPUserSolution -LiteralPath d:\temp.wsp -Site $Siteurl
$ErrorActionPreference = “silentlycontinue”
do
{
Write-Host “.” -NoNewline -ForeGroundColor White;
Start-Sleep -Seconds 5;
try
{
$testsolution = Get-SPUserSolution -Identity temp.wsp -Site $Siteurl
}
catch
{}
} while(!$testsolution);
$ErrorActionPreference = “stop”
Install-SPUserSolution -Identity temp.wsp -Site $Siteurl
Write-Host “Site collection with custom template is created”
End Part 2 Create a SharePoint Site Collection with a custom Template using PowerShell
Note that the Template GUID and Name will never change. So if you want to send the script and the package to someone else, you can hard code this part and it will work no problem.
Reference : http://itfreesupport.com/2014/12/sharepoint-2010-2013-create-a-sharepoint-site-collection-with-a-custom-template-using-powershell/