SharePoint Online: PowerShell to Create Subsite from Custom Template

Requirement: Create SharePoint Online subsite from a custom site template using PowerShell.

How to create a subsite from a custom template in SharePoint Online?

SharePoint templates can be beneficial in saving time when creating new sites or pages. You can quickly create a new site by using a template similar to what you need, and then customize it according to your requirements. In this blog post, we will show how you can create a subsite from a custom template in SharePoint Online. Also, we will see how to use PowerShell to create a subsite from a custom template in SharePoint Online. Let’s get started!

To create a subsite from a template in SharePoint Online, follow these steps:

  1. Sign-in to your SharePoint Online site >> Click on Site Settings gear Icon >> Choose “Site Contents”.
  2. On the site contents page, click on “New” >> Click on the “Subsite” link from the menu.
    sharepoint online powershell create subsite custom template
  3. Provide the Name, description, and URL to your new subsite. 
  4. Under the “Template Selection” section, select the template from the “Custom” tab
    create subsite from custom template powershell
  5. Specify other optional settings such as Permissions, Navigation, etc., and click on the “Create” button at the bottom of the page to create a subsite using a specified site template in SharePoint Online.

Let’s see how to create a site using a custom site template in SharePoint Online using PowerShell.

SharePoint Online: Create a Subsite from Custom Template using PowerShell

Assuming you have an existing site template, “PMO site Template V2” created already, here is the PowerShell to create a new site from the template:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Set Config parameters
$SiteURL = "https://crescent.sharepoint.com/"
$SubsiteURL = "branding"
$SubsiteTitle = "Intranet Branding"
$SiteTemplateName="PMO Site Template V2"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

Try {
    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Context.Credentials = $credentials

    #Get the Custom Site Template
    $SiteTemplates = $Context.Site.GetWebTemplates("1033","0")
    $Context.Load($SiteTemplates)
    $Context.ExecuteQuery()
    $Template = $SiteTemplates | Where {$_.Name -eq $SiteTemplateName}

    #Specify Subsite details and create subsite from custom template
    $Subsite  = New-Object Microsoft.SharePoint.Client.WebCreationInformation
    $Subsite.Title = $SubsiteTitle
    $Subsite.WebTemplate = $Template
    $Subsite.Url = $SubsiteURL
    $NewSubsite = $Context.Web.Webs.Add($Subsite)
    $Context.ExecuteQuery()

    Write-host "Subsite Created Successfully!" -ForegroundColor Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

This could be useful if you want to quickly create a number of sites that share some common customizations, rather than creating them manually in the SharePoint UI. Here is my other post on SharePoint On-premises create a subsite from a custom template using PowerShell: PowerShell to Create Subsite from Custom Template in SharePoint

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

3 thoughts on “SharePoint Online: PowerShell to Create Subsite from Custom Template

  • Same here.

    It is not using custom template.
    $Template is null.

    Please help.

    Reply
    • The template name specified in $SiteTemplateName variable must exist in the site collection prior to creating a site from that specific custom template. If you have saved a site as temple from a different site collection, make sure you have uploaded the site template to your target site collection first.

      Reply
  • I managed to get this working to create the subsite, however, it will not create the site using the custom template. I guess it just uses the defaults. I could delete the default folders and then use PowerShell to create them (as I use the template to create 6 folders on the subsite) but i’d like to get it working using the script above. It says completed successfully so i am not sure why its not building the subsite without using the template. Any ideas?

    Reply

Leave a Reply

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