SharePoint Online: Create Multiple Site Collections in Bulk from a CSV File using PowerShell

Requirement: Create new site collections in SharePoint Online in Bulk from a CSV File using PowerShell

How to Create site collections from a CSV file in SharePoint Online using PowerShell?

Are you a SharePoint Online administrator who needs to create multiple site collections in bulk? In this blog post, we will be looking at how to create multiple SharePoint Online site collections. This can be useful if you need to rapidly provision new environments and quickly create a large number of site collections for testing or development purposes. Either way, let’s see how to use PowerShell to create multiple SharePoint Online site collections from a CSV file.

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

  1. Site collection Title
  2. Site collection URL
  3. Site Collection Owner
  4. Storage quota, in MB
  5. Resource quota (default is 300)
  6. Site collection template (E.g., Team site template ID STS#0)
powershell to create site collections from csv in sharepoint online

Step 2: Use this PowerShell script to read from CSV, create site collections, and run with SharePoint Online Management Shell.

#Function to Create Site Collection
Function Create-SPOSite 
{
  param
    (
        [string]$Title  = $(throw "Please Provide the Site Title!"),
        [string]$URL = $(throw "Please Provide the Site URL!"),
        [string]$Owner = $(throw "Please Provide the Site Owner!"),
        [int]$StorageQuota = $(throw "Please Provide the Site Storage Quota!"),
        [int]$ResourceQuota = $(throw "Please Provide the Site Resource Quota!"),
        [string]$Template = $(throw "Please Provide the Site Template!")
    )

#Connection parameters 
$AdminURL = "https://crescent-admin.sharepoint.com"

#Get Credentials to connect
$Cred= Get-Credential -Message "Enter the Admin Credentials"
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $Cred.Username, $Cred.Password
 
Try{
    #Connect to Office 365
    Connect-SPOService -Url $AdminURL -Credential $Credentials
 
    #Check if the site collection exists already
    $SiteExists = Get-SPOSite | where {$_.url -eq $URL}
    #Check if site exists in the recycle bin
    $SiteExistsInRecycleBin = Get-SPODeletedSite | where {$_.url -eq $URL}

    If($SiteExists -ne $null)
    {
        write-host "Site $($url) exists already!" -foregroundcolor red
    }
    elseIf($SiteExistsInRecycleBin -ne $null)
    {
        write-host "Site $($url) exists in the recycle bin!" -foregroundcolor red
    }
    else
    {
        #create the site collection
        New-SPOSite -Url $URL -title $Title -Owner $Owner -StorageQuota $StorageQuota -NoWait -ResourceQuota $ResourceQuota -Template $Template
        write-host "Site Collection $($url) Created Successfully!" -foregroundcolor Green
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
}

#Read from CSV and create site collection
Import-Csv "C:\SitesToCreate.csv" | Foreach-Object {
   Create-SPOSite -Title $_.SiteTitle -URL $_.SiteURL -Owner $_.SiteOwner -StorageQuota $_.StorageQuota -ResourceQuota $_.ResourceQuota -Template $_.SiteTemplate  }

Creating SharePoint Online site collections using PowerShell is in my other article: How do I create a SharePoint Online site collection using PowerShell?

PnP PowerShell: Create Multiple Sites from a CSV File

We can also use PnP PowerShell to bulk provision SharePoint Online site collections. Here is my CSV file format.

sharepoint online create multiple site collections from a CSV file using PowerShell

You can download the CSV file from:

PnP PowerShell Script to Create Site Collections in Bulk:

This script will create multiple SharePoint Online Site Collections from the CSV file provided, using New-PnPTenantSite cmdlet.

#Set Config Parameters
$TenantUrl = "https://crescent-admin.sharepoint.com"
$CSVPath = "C:\Temp\SiteCollections.csv"

#Connect to Tenant
Connect-PnPOnline -url $TenantUrl -Interactive

Try {
    #Get Site Collections to create from a CSV file
    $SiteCollections = Import-Csv -Path $CSVPath

    #Loop through csv and create site collections from each row
    ForEach ($Site in $SiteCollections)
    {
        #Get Parameters from CSV
        $Url = $Site.Url
        $Title = $Site.Title
        $Owner = $Site.Owner
        $Template = $Site.Template
        $TimeZone = $Site.TimeZone

        #Check if site exists already
        $SiteExists = Get-PnPTenantSite | Where {$_.Url -eq $URL} 
        If ($SiteExists -eq $null)
        {
            #Create site collection
            Write-host "Creating Site Collection:"$Site.URL -f Yellow
            New-PnPTenantSite -Url $URL -Title $Title -Owner $Owner -Template $Template -TimeZone $TimeZone -RemoveDeletedSite -ErrorAction Stop        
            Write-host "`t Done!" -f Green
        }
        Else
        {
            write-host "Site $($URL) exists already!" -foregroundcolor Yellow
        }
    }
}
Catch {
    write-host -f Red "`tError:" $_.Exception.Message
}

Here is another article for a similar requirement in SharePoint On-Premises: How to Create SharePoint Site Collections in Bulk from a CSV File using PowerShell?

Is there a limit to the number of SharePoint Online sites? How many SharePoint sites can I create?

Yes! 2 Million per tenant.

Can we create a subsite in SharePoint online?

Yes! You can still create subsites in SharePoint Online, although Hub sites are the recommended replacements for subsites.
More info: How to Create a subsite in SharePoint Online?

How do I Get all site collections in SharePoint Online?

Use SharePoint Admin Center or PowerShell script to get all sites in the SharePoint Online environment.
More info: SharePoint Online PowerShell to get all site collections

How to create a communication site in SharePoint Online?

To create a communication site, Login to SharePoint Admin Center >> Click on “Sites”>> “Active Sites”>> Click on “Create”>> Choose “Communication Site”, Enter the site name, validate the site address and Click on Finish. You can also create communication sites using PowerShell.
More info: Create a communication site in SharePoint Online with PowerShell

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!

Leave a Reply

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