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

Requirement: Create Multiple sub-sites in SharePoint Online using PowerShell.

SharePoint Online: How to Create Multiple Subsites using PowerShell?
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 = ""

#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

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: Bulk Subsite creation CSV Template

PowerShell to Create Subsites from CSV
This PnP PowerShell script reads the CSV file and creates subsite on given site collection from details in the CSV.
#Function to Create Subsite
Function Create-PnPSubsite
        [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 = "",
        [Parameter(Mandatory=$false)] [string]$BreakInheritance = "False",
        [Parameter(Mandatory=$true)] [string]$Template = $(throw "Please Provide the Site Template!")
        #Connect to PnP Online
        Connect-PnPOnline -Url $SiteCollectionURL -Credentials $Cred #-UseWebLogin

        #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
            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 

1 comment:

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

    At C:\Users\TechnoMatters\OneDrive - TechnoMatters\Desktop\Migration\SP\Subsite creation.ps1:23 char:110
    + ... ue)] [string]$Template = $(throw "Please Provide the Site Template!")
    + ~
    Missing closing ')' in expression.
    At C:\Users\TechnoMatters\OneDrive - TechnoMatters\Desktop\Migration\SP\Subsite creation.ps1:15 char:1
    + {
    + ~
    Missing closing '}' in statement block or type definition.
    At C:\Users\TechnoMatters\OneDrive - TechnoMatters\Desktop\Migration\SP\Subsite creation.ps1:24 char:9
    +     )
    + ~
    Unexpected token ')' in expression or statement.
    At C:\Users\TechnoMatters\OneDrive - TechnoMatters\Desktop\Migration\SP\Subsite creation.ps1:46 char:1
    + }
    + ~
    Unexpected token '}' in expression or statement.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingEndParenthesisInExpression


Please Login and comment to get your questions answered!

Powered by Blogger.