8

I have found a PowerShell script to create a site collection with the template "STS#0", but i want to create a root site collection with a custom template, what will be the possible PowerShell command?

  • 1
    Is this for SharePoint 2013 or SharePoint Online? – James Grizzle Mar 2 '15 at 16:02
  • It is for SharePoint 2013. – SPBeginer Mar 2 '15 at 17:00
3

Here is how you create a new site using a custom .wsp template with PowerShell.

     $mainurl = "http://<WEBAPPURL>/sites/<NAME_OF_SITECOLLECTION>"
     Write-Host -ForegroundColor White " – Creating Site Collection  $mainurl…" -NoNewline
     New-SPSite $mainurl -OwnerAlias locadministrator  -Name "This will have a custom template"
     Write-Host -ForegroundColor GREEN "Done."

Afterwards, we have to add our WSP to the Solution Gallery in the Site Collection. Use Do While loop to make sure that the solution has been uploaded successfully before activating it.

Write-Host -ForegroundColor White " – Adding and Installing Your Custom Template…" –NoNewline]
Add-SPUserSolution -LiteralPath <PHYSICAL_PATH_TO_WSP> -Site $mainurl
         $ErrorActionPreference = "silentlycontinue"
do
{
  Write-Host "." -NoNewline -ForeGroundColor White;
  Start-Sleep -Seconds 5;                                
  try
  {
      $testsolution = Get-SPUserSolution -Identity <WSPNAME>.wsp -Site $mainurl
  }
  catch{}
}while(!$testsolution);
$ErrorActionPreference = "stop"
Install-SPUserSolution -Identity <WSPNAME>.wsp -Site $mainurl
Write-Host -ForegroundColor GREEN "Done."

Now that we got our empty site collection, we need to find the ID of the template we want to apply to it. Here is the Command.

$site= new-Object Microsoft.SharePoint.SPSite($mainurl )
$loc= [System.Int32]::Parse(1033)
$templates= $site.GetWebTemplates($loc)
foreach ($child in $templates)
{
    write-host $child.Name "" $child.Title
}
$site.Dispose()

At the end of the list, you will find your Custom Template GUID and Name.
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 hardcode this part and it will work no problem. The last part, is applying the template to our new Site.

$web = GET-SPWeb $mainurl
$web.ApplyWebTemplate("{<GUID>}#<CUSTOMTEMPLATE>")

After, you can access your site and your custom template will be applied.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.