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 = "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
}

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
{
  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 = "",
        [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 #-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
        }
        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 
}
SharePoint Online: Create Multiple Subsites from a CSV File using PowerShell SharePoint Online: Create Multiple Subsites from a CSV File using PowerShell Reviewed by Salaudeen Rajack on May 06, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.