SharePoint Online: Create Multiple Subsites from a CSV File using PowerShell

Requirement: Create Multiple subsites in SharePoint Online using PowerShell.

SharePoint Online: How to Create Multiple Subsites using PowerShell?

Creating multiple subsites in SharePoint Online can be a tedious process and can be a lot of work if you have to do it one by one. Wouldn’t it be great if there was a way to automate the process? Fortunately, there is a PowerShell script that will make the process much easier. I’ll show you how to use PowerShell to bulk-create subsites in this post.

Creating a subsite in SharePoint Online with PowerShell is explained in my other article: How to Create a Subsite using PowerShell in SharePoint Online?, You can create multiple subsites in SharePoint Online by a simple array and looping as:

#Site collection URL
$SiteURL = "https://crescent.sharepoint.com/sites/intranet"

#Connect to Pnp Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Define subsites in array
$Subsites = "Sales","Production", "Marketing", "Support"

#Iterate through each site
ForEach($Site in $Subsites)
{
    #Create subsite
    New-PnPWeb -Title $Site -Url $Site -InheritNavigation -Template STS#3
}

This can be helpful if you need to create a large number of subsites and don’t want to do it manually.

PowerShell to Create Subsites from CSV File in SharePoint Online

Create a CSV file in the below format. Populate the column values according to your requirements. For each subsite, set the below properties:

  1. SiteCollectionURL – Parent site collection for subsite
  2. SubSiteTitle – Name of the subsite
  3. Description – Subsite Description
  4. URL – Subsite URL
  5. BreakInheritance – By default, Subsite Inherits Permissions from Parent
  6. Template – Template of the Sub-Site 

Here is my CSV File

SharePoint Online Create Multiple Subsites from a CSV File using PowerShell

You can download CSV File here:

PowerShell to Create Subsites from CSV

This PnP PowerShell script reads the CSV file and creates a subsite on the given site collection from details in the CSV.

#Function to Create Subsite
Function Create-PnPSubsite
{
  param
    (
        [Parameter(Mandatory=$true)] [string]$SiteCollectionURL  = $(throw "Please Enter the Site Collection URL!"),
        [Parameter(Mandatory=$true)] [string]$Title  = $(throw "Please Enter the Subsite Title!"),
        [Parameter(Mandatory=$true)] [string]$URL = $(throw "Please Enter the Subsite URL!"),
        [Parameter(Mandatory=$false)] [string]$Description = [string]::Empty,
        [Parameter(Mandatory=$false)] [string]$BreakInheritance = "False",
        [Parameter(Mandatory=$true)] [string]$Template = $(throw "Please Provide the Site Template!")
    )
    Try{
        #Connect to PnP Online
        Connect-PnPOnline -Url $SiteCollectionURL -Credentials $Cred #-Interactive

        #Check if subsite exists
        $Subsite = Get-PnPWeb -Identity $URL -ErrorAction SilentlyContinue
        If($Subsite -eq $null) 
        {
            #Create subsite
            $BreakInheritanceFlag = [System.Convert]::ToBoolean($BreakInheritance) 
            $SubSite = New-PnPWeb -Title $Title -Url $URL -Description $Description -BreakInheritance:$BreakInheritanceFlag -Template $Template 
            Write-Host "Created Subsite:"$URL -ForegroundColor Green
        }
        Else
        {
            Write-Host "Subsite already exist:"$URL -ForegroundColor Yellow
        }
    }
    catch {
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
}

#Set CSV File path
$CSVFilePath = "C:\Temp\Subsites.csv"

#Get Credentials to connect
$Cred = Get-Credential

#Read from CSV and call the function to create subsites
Import-Csv $CSVFilePath | ForEach-Object {
   Create-PnPSubsite -SiteCollectionURL $_.SiteCollectionURL -Title $_.SubSiteTitle -URL $_.URL -Description $_.Description -BreakInheritance $_.BreakInheritance -Template $_.Template 
}

To create a subsite with unique permissions, use: How to Create a SharePoint Online Subsite with Unique Permissions using PowerShell?

In summary, using PowerShell to create multiple subsites in SharePoint Online is an efficient way to automate the process of creating bulk subsites! Additionally, by utilizing the PowerShell scripts, CSV file, and loops, you can create multiple subsites at once, and save time.

How to enable subsite creation in SharePoint Online?

In modern SharePoint Online sites, subsite creation is disabled by default. To enable subsite creation: Login to SharePoint Online Admin Center >> Click on Settings >> Click on “Classic settings page”. On the classic settings page, under “Subsite creation”, Select “Show the Subsite command for all sites” to turn on SharePoint subsite creation on all sites.
More info: Enable Subsite Creation in SharePoint Online

How to Delete a Subsite in SharePoint Online?

To delete a subsite from SharePoint Online: Click on Site Settings >> Choose “Site Settings”. Click on the “Delete this site” link under the “Site Actions” group. Click on the “Delete” button and confirm subsite deletion, and you’ll get a “Your Web site has been deleted.” message for a while.
More info: Delete a Subsite in SharePoint Online

How to Restore a deleted subsite in SharePoint Online?

To restore a subsite, follow these steps: Navigate to Site Settings >> Click on “Recycle bin” >> Click on the “Second-stage recycle bin” link and select the desired subsite by clicking the checkbox of the subsite, click on the “Restore” button in the ribbon to start restoring the subsite.
More info: Restore a subsite in SharePoint Online

How to change the subsite URL in SharePoint Online?

There are two different ways to do this, and I will show you two methods. The first method is the easiest, from the web browser interface, and the second method is with PowerShell.
More info: Change subsite URL SharePoint Online using PowerShell

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!

2 thoughts on “SharePoint Online: Create Multiple Subsites from a CSV File using PowerShell

  • Hello there. Keep getting the below error. All i have done is edited the csv and updated the path. Any clues please?

    At C:UsersTechnoMattersOneDrive – TechnoMattersDesktopMigrationSPSubsite creation.ps1:23 char:110
    + … ue)] [string]$Template = $(throw “Please Provide the Site Template!”)
    + ~
    Missing closing ‘)’ in expression.
    At C:UsersTechnoMattersOneDrive – TechnoMattersDesktopMigrationSPSubsite creation.ps1:15 char:1
    + {
    + ~
    Missing closing ‘}’ in statement block or type definition.
    At C:UsersTechnoMattersOneDrive – TechnoMattersDesktopMigrationSPSubsite creation.ps1:24 char:9
    +     )
    + ~
    Unexpected token ‘)’ in expression or statement.
    At C:UsersTechnoMattersOneDrive – TechnoMattersDesktopMigrationSPSubsite creation.ps1:46 char:1
    + }
    + ~
    Unexpected token ‘}’ in expression or statement.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingEndParenthesisInExpression

    Reply

Leave a Reply

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