SharePoint 2010 \ 2013 Create a SharePoint Site Collection with a custom Template using Powershell
Save the custom site with all custom list\library etc as .wsp. To do so follow below:
1. Go to Site action > Site settings > Save site as template
- Mention the name of template and select “Include content” > Click OK
-
Once the site is saved, you will get link for “Solution gallery”. Go to Solution gallery and download the teamplate (temp.wsp) file to something like d:\temp.wsp.
You need to write powershell in 2 parts. One will create site collection and 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.