SharePoint Online: PowerShell to Create Subsite from Custom Template

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

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

To add a subsite in SharePoint online, follow these steps:

  • Sign-in to your SharePoint online site >> Click on Site Settings gear Icon >> Choose “Site Contents”
  • On the site contents page, click on “New” >> Click on “Subsite” link from the menu.
    sharepoint online powershell create subsite custom template
  • Provide the Name, description, and URL to your new subsite. 
  • Under the “Template Selection” section, select the template from the “Custom” tab
    create subsite from custom template powershell
  • 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 custom site template in SharePoint Online using PowerShell.

SharePoint Online: Create 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 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
}

Here is my another post on SharePoint On-premises create subsite from custom template using PowerShell: PowerShell to Create Subsite from Custom Template in SharePoint

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

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

  • October 15, 2021 at 4:01 PM

    Same here.

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

    Please help.

    Reply
  • March 10, 2021 at 10:24 PM

    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