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:
- SiteCollectionURL – Parent site collection for subsite
- SubSiteTitle – Name of the subsite
- Description – Subsite Description
- URL – Subsite URL
- BreakInheritance – By default, Subsite Inherits Permissions from Parent
- Template – Template of the Sub-Site
Here is my CSV File
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
}
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
Looks the CSV has Unicode characters in it. Try: Import-Csv $CSVFilePath -Encoding UTF8