SharePoint Online: How to Create a Site Collection using PowerShell?

Requirement: Create a new site collection in SharePoint Online using PowerShell.

How to Create a Site Collection in SharePoint Online?

In this guided walkthrough, I’ll show you how to use PowerShell to create a new SharePoint Online site collection. Undoubtedly, PowerShell is the faster and more efficient way to create site collections in SharePoint Online. Let’s get started!

Site collection in SharePoint is a logical group of sites with a common top-level site and subsites organized as a hierarchy. Every site collection shares a common top site, navigation, security, content types, etc. To create a modern SharePoint Online Site collection, follow these steps:

  1. Login to SharePoint Admin Center as a tenant admin or SharePoint Online Administrator permissions. 
  2. Click on Sites >> Active Sites from left navigation >> Click on “Create”.
  3. Pick either the “Team Site” option to create a modern team site collection with Office 365 group or the “Other Options” to create a modern team site without Office 365 group. create modern site collection in sharepoint online
  4. From the SharePoint site templates, pick the site template such as “Team site”, provide the site name, site address, and Administrator options, and select a language. Click on “Advanced settings” for the time zone and site description. Click on “Finish” once you entered all details and create a site collection in SharePoint Online.
    sharepoint online create modern team site collection

Wait for a moment, and your modern SharePoint site collection should appear in the site collections list.

How to create a SharePoint Online site from the SharePoint Start page?

You can also use the SharePoint Home page (https://tenant.sharepoint.com/_layouts/15/sharepoint.aspx) to create a SharePoint site by simply going to Office 365 App Launcher >> Click on SharePoint >> Click on the “Create site” button in the toolbar. (Unless the site creation is disabled by the SharePoint Administrators or Global Administrators – By default, it’s enabled! So, anyone can create a new site).

create a new site collection in sharepoint online

Enter the site name, description, Privacy settings, and language, set the group email address, and click on Next >> Add any group members to grant site permissions and click on Finish. This creates a Microsoft 365 group-connected SharePoint site collection.

how to create a new site collection in sharepoint online

Creating a site collection from the SharePoint admin center or from the SharePoint Home site is a relatively simple task, isn’t it? Now, let’s see how to create a site collection in SharePoint Online using PowerShell.

SharePoint Online: Create Modern Team Site Collection using PowerShell

A team site is the most common type of site template used in SharePoint. As the name suggests, team sites are created for team collaboration for individual teams, departments, functional groups, etc. You can create a modern site collection in SharePoint Online by specifying the site template as “STS#3”. Here is how to create a new SharePoint Online site with PowerShell:

#Connect to SharePoint Online
Connect-SPOService -url "https://crescent-admin.sharepoint.com" -Credential (Get-credential)
 
#Create a modern team site
New-SPOSite -Url "https://crescent.sharepoint.com/sites/Purchase" -Owner "Salaudeen@Crescent.com" -StorageQuota 2048 -Title "Purchase Team Site" -Template "STS#3"
The maximum number of site collections created on an Office 365 tenant is 2 Million!
create a site collection in sharepoint online using powershell

Now, let us add some error handling and wrap it inside a re-usable function to create a site using PowerShell in SharePoint Online.

Create a SharePoint Online Site Collection using PowerShell:

Let’s add some error handling and make the script bit more flexible for SharePoint Online to create a site collection with PowerShell. 

#powershell to create site collection sharepoint online
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"
$AdminName = "SpAdmin@Crescent.com"
$AdminPassword="Password Goes here"
$SecurePassword = $AdminPassword | ConvertTo-SecureString -AsPlainText -Force 
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $AdminName, $SecurePassword
 
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
    {
        #sharepoint online create site collection powershell
        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
    }
}

#Parameters to create new site collection
$SiteTitle = "Demo"
$SiteURL= "https://Crescent.sharepoint.com/sites/demos"
$SiteOwner = "spsadmin@Crescent.com"
$StorageQuota = 1000
$ResourceQuota = 300
$SiteTemplate = "STS#3"

#Call The function to create new sharepoint online site using powershell
Create-SPOSite -Title $SiteTitle -URL $SiteURL -Owner $SiteOwner -StorageQuota $StorageQuota -ResourceQuota $ResourceQuota -Template $SiteTemplate 

Now the new site collection will be created in a minute! You can verify by going to the SharePoint Online Admin Center.

PowerShell to Create New Site Collection in SharePoint Online using CSOM

A SharePoint Online site collection is a group of related sites, web pages, document libraries, and lists for data management. Here is the SharePoint Online PowerShell to create a new site collection.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"
 
#Set Parameters
$AdminCenterURL = "https://Crescent-admin.sharepoint.com/"
$NewSiteURL = "https://Crescent.sharepoint.com/Sites/HR"

Try {
    #Setup Credentials to connect
    $Cred= Get-Credential
     
    #Setup the Context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminCenterURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Get the tenant object 
    $Tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($Ctx)

    Write-Host -f Yellow "Creating site collection..."
    #Set the Site Creation Properties
    $SiteCreationProperties = New-Object Microsoft.Online.SharePoint.TenantAdministration.SiteCreationProperties
    $SiteCreationProperties.Url = $NewSiteURL
    $SiteCreationProperties.Template =  "STS#0" #Classic site
    $SiteCreationProperties.Owner = "salaudeen@Crescenttech.com"
    $SiteCreationProperties.StorageMaximumLevel = 1000
    $SiteCreationProperties.UserCodeMaximumLevel = 300
 
    #powershell script to create site collection in sharepoint online
    $Tenant.CreateSite($SiteCreationProperties) | Out-Null
    $ctx.ExecuteQuery()
 
    #Create the site in the tennancy
    write-host "Site Collection Created Successfully!" -foregroundcolor Green
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

To create multiple SharePoint Online site collections in bulk from a CSV file, refer: SharePoint Online: Create Multiple Site Collections in Bulk from a CSV File using PowerShell

SharePoint Online: PnP PowerShell to Create Site Collection

How do I create a new site collection in SharePoint Online using PowerShell? Here is how to create a site collection using PnP PowerShell in SharePoint Online. You can also use the New-PnPSite cmdlet to create Modern Team sites and communication sites.

#Define Variables
$AdminCenterURL = "https://Crescent-admin.sharepoint.com"
$SiteURL = "https://Crescent.sharepoint.com/sites/procurement"
$SiteTitle = "Crescent Procurement Portal"
$SiteOwner = "Salaudeen@TheCrescentTech.com"
$Template = "STS#3" #Modern SharePoint Team Site
$Timezone = 4

#Get Credentials to connect
$Cred = Get-Credential

Try
{
    #Connect to Tenant Admin
    Connect-PnPOnline -URL $AdminCenterURL -Credential $Cred
    
    #Check if site exists already
    $Site = Get-PnPTenantSite | Where {$_.Url -eq $SiteURL}

    If ($Site -eq $null)
    {
        #sharepoint online pnp powershell create a new team site collection
        New-PnPTenantSite -Url $SiteURL -Owner $SiteOwner -Title $SiteTitle -Template $Template -TimeZone $TimeZone -RemoveDeletedSite
        write-host "Site Collection $($SiteURL) Created Successfully!" -foregroundcolor Green
    }
    else
    {
        write-host "Site $($SiteURL) exists already!" -foregroundcolor Yellow
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

To create a classic team site in SharePoint Online, use: SharePoint Online: Create a Classic Site using PowerShell

How to create a subsite in SharePoint Online using PowerShell?

There are a few different ways to create subsites in SharePoint Online. The easiest way is to use the web browser and create the subsite. You can also use the PowerShell script to add a new subsite to SharePoint Online.
More info: How to Create a Subsite in SharePoint Online?

How do I create a communication site in SharePoint Online?

Creating a communication site in SharePoint online is easy and can be done in just a few minutes. You can start adding a new communication site by the web browser as SharePoint/Tenant Admin, from the SharePoint Start page, or utilize PowerShell to create a communication site in SharePoint Online.
More info: Create a Communication site in SharePoint Online

How do I add a collections admin in SharePoint online?

To give a site admin rights in SharePoint Online, use the PowerShell cmdlet Set-SPOUser or the PnP PowerShell cmdlet Set-PnPTenantSite. You can also add a user as a collections administrator from the SharePoint Admin center or Site settings page.
More info: Add a site collection admin in SharePoint Online

How do I create Hub sites in SharePoint online?

To create a Hub Site in SharePoint Online, follow these steps: Navigate to the SharePoint Online Admin Center >> Click on Active Sites >> Select the site that will become a hub >> Click on “Register as Hub Site” from the Hub Site menu. Fill in the name for the Hub and click on Save. You can also create a hub site using PowerShell.
More info: How to Create a hub site in SharePoint Online?

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!

One thought on “SharePoint Online: How to Create a Site Collection using PowerShell?

Leave a Reply

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