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  }

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.

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

#Connect to Tenant
Connect-PnPOnline -url $TenantUrl -Credentials (Get-Credential) #or -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

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

Leave a Reply