SharePoint Online: How to Create a Subsite using PowerShell?

Requirement: Create a Subsite in SharePoint Online using PowerShell

How to Create a Subsite in SharePoint Online?

If you are a SharePoint administrator or site owner, eventually you will need to create a subsite. In this blog post, I’ll walk through the process of creating a subsite in SharePoint Online using PowerShell and the steps necessary for creating your subsite from scratch using the web browser interface.

Subsites in SharePoint Online can be created within any existing root site or inside another subsite. By default, site owners (or users with full control or Manage Hierarchy) have permission to create subsites. To add a subsite in SharePoint Online, follow these steps:

Create subsite option is missing in SharePoint Online?
As Microsoft recommends to use SharePoint hub sites, verify in the SharePoint Admin center, if the Subsite creation is enabled by the SharePoint Admins in your tenant before proceeding with these steps. You may not get the option to create a subsite in the command bar, if it is disabled! Not only the new subsite creation command, but it also disables the ability to add new Subsite through Rest API and CSOM methods. How to Enable or Disable Subsite Creation in SharePoint Online?
  • Sign in to your SharePoint Online site as Site Collection Administrator/Site Owner >> Navigate to the site under which the new subsite is to be created.
  • Click on Site Settings Gear Icon >> Choose “Site Contents” (or Click on the Site Content link from the Quick Launch) in the classic sites.
    sharepoint online powershell create subsite
  • Scroll down to the bottom and click on “Create Subsite” link (URL shortcut: /_layouts/15/newsbweb.aspx)
    sharepoint online powershell create web
  • How do I create a subsite in Modern SharePoint? In modern sites, this link is moved to the “New” menu at the toolbar. If you don’t find the “Subsite” link on the site contents page, that could be because Subsite creation is disabled at the tenant level. how to create a subsite in sharepoint online using powershell
  • Provide the Name, description, Site URL, and template for your new subsite. Specify other subsite settings like Permissions to inherit from the parent site or to use unique permissions and Navigation inheritance – Whether the Quick Launch and top link bar of the parent site are to be inherited in the subsite. A subsite can use either a different or the same template as its parent or root site. how to create subsite in sharepoint online
  • Click on the Create button at the bottom of the page to create a subsite in SharePoint Online.
It’s the best practice to exclude special characters and spaces from the URL of the site! E.g. If your site title is Human Resources, the URL name should be either HumanResources or HR.

You can also create a SubSite from the “Sites and Workspaces” page in the site settings.

Is there a limit to the number of subsites in SharePoint? Yes, You can create up to 2000 subsites per site collection under any subsite or root site. Once the subsite is created, you’ll be taken to the home page of the new subsite. PowerShell is the answer if you are looking for a quick, easy way to create subsites in Sharepoint Online! Let’s see how to create a subsite in SharePoint Online using PowerShell script.

SharePoint Online: PowerShell to Create Subsite

Let’s create a subsite in SharePoint Online using PowerShell CSOM. This solution is for those who need to automate the creation of subsites from their script or other automation processes.

#Load SharePoint Online 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"

#Variables for processing
$SiteURL = "https://crescent.sharepoint.com/Sites/Sales"
$AdminAccount = "[email protected]"
 
#User Names Password to connect 
$Password = Read-host -assecurestring "Enter Password for $AdminAccount" 
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminAccount, $Password

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

    #Specify Subsite details for powershell script to create subsite in sharepoint online
    $WebCI = New-Object Microsoft.SharePoint.Client.WebCreationInformation
    $WebCI.Title = "Sales - US "
    $WebCI.WebTemplate = "STS#0" #Classic Team Site
    $WebCI.Url = "us"
    #sharepoint online create subsite powershell
    $SubWeb = $Context.Web.Webs.Add($WebCI)
    $Context.ExecuteQuery()

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

This PowerShell script creates a new web in SharePoint Online. I’ve used only mandatory parameters. You can refer to this Technet link if you want to specify other parameters such as permission, navigation settings, etc. https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.client.webcreationinformation_members.aspx

Create Subsite in SharePoint Online using PnP PowerShell

Sharepoint Online PnP PowerShell module helps to simplify administration tasks, such as creating subsites. To create a new subsite in the current site collection, use the New-PnPWeb cmdlet. Here is how to create a subsite using PnP PowerShell in SharePoint Online:

#Config Variables
$SiteCollURL = "https://Crescent.sharepoint.com"
$SiteTitle = "Town Hall 2017"
$SiteURL = "townhall2017"
$Description = "Town Hall 2017 site - Find all Videos, Presentations, Demos Shared here!"
$Locale = 1033 #English
$Template = "STS#3" #Modern Subsite Template - Team site

#Get Credentials to connect
$Cred = Get-Credential

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteCollURL -Credentials $Cred

    #sharepoint online powershell create subsite
    New-PnPWeb -Title $SiteTitle -Url $SiteURL -Description $Description -Locale $Locale -Template $Template -ErrorAction Stop
    Write-host "Site '$SiteTitle' Created Successfully!" -f Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

Additionally, The New-PnPWeb cmdlet supports -BreakInheritance, -InheritNavigation switches to set up site groups such as Owners group, members groups for the subsite.

How to create a communication subsite in SharePoint Online? Well, just use the $Template as “SITEPAGEPUBLISHING#0” in the above PowerShell script. The web browser interface doesn’t support creating a subsite of communication template.

To create a subsite from a custom template, use: PowerShell to Create Subsite from Custom Template in SharePoint Online

How to Create Multiple Subsites in SharePoint Online from a CSV?

Create a CSV file and populate the columns, such as SiteCollectionURL, SubSiteTitle, URL, etc., as per your requirements. The PowerShell script can read the CSV file and creates a subsite on the given site collection from details in the CSV.
More info: Bulk create subsites in SharePoint Online from a CSV file using PowerShell

How to change the subsite URL in SharePoint Online?

A subsite’s URL can be changed either from the web browser interface or using PowerShell.
More info: How to Change the URL of a Subsite in SharePoint Online?

How to check the subsite template in SharePoint Online?

To check your site template: Open your SharePoint Online subsite in the browser, and Go to View >> Page Source. In the page source, search for “webTemplateConfiguration”, and you’ll find the site template value next. You can also use PowerShell to find out the web template of your site.
More info: How to Find the template of your SharePoint Online subsite?

How to copy a subsite in SharePoint Online?

With PnP PowerShell’s provisioning template method, you can duplicate any artifacts in SharePoint Online, like a subsite, list, etc.
More info: How to copy a subsite using the PnP PowerShell?

How to delete subsite in SharePoint online using PowerShell?

While removing subsites in SharePoint Online using web browser UI is relatively simpler, We may need PowerShell in some instances, such as recursive deletion. You can use either the CSOM method $web.DeleteObject() or PnP PowerShell cmdlet Remove-PnPWeb to remove a SharePoint Online subsite.
More info: PowerShell to delete a subsite in SharePoint Online

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

8 thoughts on “SharePoint Online: How to Create a Subsite using PowerShell?

Leave a Reply

Your email address will not be published.