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?
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 and 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 provision SharePoint Online site collections in bulk. 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: SharePoint Online: Multiple Site Creation Template

PnP PowerShell Script to Create Site Collections in Bulk:
#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 -UseWebLogin

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
}

This script will create multiple SharePoint Online Site Collections from the .Csv file provided. Here is my another article for similar requirement in SharePoint On-Premises: How to Create SharePoint Site Collections in bulk from a CSV File using PowerShell

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.