Create Site Collection in SharePoint online using PowerShell

create subsite in sharepoint online using powershell

In this SharePoint Online, I will explain how to create a site collection using PowerShell in SharePoint Online Office 365.  You can learn the process of provisioning Site Collection in SharePoint Online Office 365 using PowerShell.

In my previous post, I have explained How to retrieve all site templates available in SharePoint Online using PowerShell? We are going to use site template id while creating the site collection in SharePoint Online using PowerShell.


Prerequisites:
What we need to achieve this scenario is:

  • Basic knowledge in PowerShell
  • SPO Admin URL & Access
  • New Site Collection URL & Owner name
  • Site collection template ID

List of Site Collection templates in SharePoint Online:

Below is the list of site collection templates which are available in SharePoint Online. Suppose you want to create a SharePoint Online Modern Team site, then you have to use STS#3 template id. Similarly, if you want to create a classic team site, then you can use STS#0 template id.

Site Collection Template ID Template Name
STS#3 Team site
STS#0 Team site (classic experience)
BLOG#0 Blog
BDR#0 Document Center
DEV#0 Developer Site
OFFILE#1 Records Center
EHS#1 Team Site – SharePoint Online configuration
BICenterSite#0 Business Intelligence Center
SRCHCEN#0 Enterprise Search Center
BLANKINTERNETCONTAINER#0 Publishing Portal
ENTERWIKI#0 Enterprise Wiki
PROJECTSITE#0 Project Site
PRODUCTCATALOG#0 Product Catalog
COMMUNITY#0 Community Site
COMMUNITYPORTAL#0 Community Portal
SRCHCENTERLITE#0 Basic Search Center
visprus#0 Visio Process Repository
SRCHCENTERLITE#0 Basic Search Center
visprus#0 Visio Process Repository

PowerShell Script to create site collection in SharePoint Online

Below is the PowerShell script to create site collection in SharePoint Online Office 365. You can write, test and debug the PowerShell script using Visual Studio Code or by using Windows PowerShell ISE.

function SPO-CreateNewSiteCollection {
#Parameters for Creating New Site Collection in SharePoint Online
param ($strNewSiteUrl, $strNewSiteTitle,$strNewSiteOwner,$strNewSiteQuota,$strNewSiteResourceQuota, $strNewSiteTemplateID, $strTenantAdminURL,$strTenantAdminName)

# Reading password in the PowerShell window
$password = Read-Host “Please enter the password for $($strTenantAdminName)” -AsSecureString
# Settingup the credentials
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $strTenantAdminName, $password
#Connecting to to SharePoint Online
try {
Connect-SPOService -Url $strTenantAdminURL -Credential $credentials
write-host “Report: Connected succesfully to SharePoint Online !!!” -foregroundcolor green
}
catch {
write-host “Error: Could not connect to SharePoint Online !!!” -foregroundcolor red
Break SPO-CreateNewSiteCollection
}
#verifying incase the site collection is already exists in SharePoint Online
$siteExists = get-SPOSite | where {$_.url -eq $strNewSiteUrl}
#verify if site collection exists even in the recycle bin
$siteExistsInRecycleBin = get-SPODeletedSite | where {$_.url -eq $strNewSiteUrl}
#Creating Site Collection by validting flag bits for above 2 scenarios
if (($siteExists -eq $null) -and ($siteExistsInRecycleBin -eq $null)) {
write-host “Report: Creating Site Collection $($strNewSiteTitle)” -foregroundcolor green
New-SPOSite -Url $strNewSiteUrl -title $strNewSiteTitle -Owner $strNewSiteOwner -StorageQuota $strNewSiteQuota -NoWait -ResourceQuota $strNewSiteResourceQuota -Template $strNewSiteTemplateID
}
elseif ($siteExists -eq $true) {
write-host “Report: $($strNewSiteUrl) already exists” -foregroundcolor red
}
else {
write-host “Report: $($strNewSiteUrl) still exists in the recyclebin” -foregroundcolor red
}
write-host “Report: Site Collection $($strNewSiteTitle) Created Successfully !!!” -foregroundcolor green
}
try {
# variables that are needed to host the new Site Collection
$strNewSiteUrl = Read-Host “Please enter URL for new Site Collection:: ex: https://onlysharepoint2013.sharepoint.com/sites/testSite”
$strNewSiteTitle = Read-Host “Name of New Site Collection:”
$strNewSiteOwner = Read-Host “Provide Admin account for new Site Collection:”
$strNewSiteQuota = 1000
$strNewSiteResourceQuota = 50
$strNewSiteTemplateID = Read-Host “Provide site Template-ID ex:STS#0”
$strTenantAdminURL = Read-Host “Provide Admin host URL: ex:https://<<tenant Name>>-admin.sharepoint.com/”
$strTenantAdminName = Read-Host “Provide Admin account ex:<<admin-user>>@<<tenant.onmicrosoft.com”

SPO-CreateNewSiteCollection -strNewSiteUrl $strNewSiteUrl -strNewSiteTitle $strNewSiteTitle -strNewSiteOwner $strNewSiteOwner -strNewSiteQuota $strNewSiteQuota -strNewSiteResourceQuota $strNewSiteResourceQuota -strNewSiteTemplateID $strNewSiteTemplateID -strTenantAdminURL $strTenantAdminURL -strTenantAdminName $strTenantAdminName
}
catch {
write-host “Error: $($strNewSiteUrl) still exists in the recyclebin” -foregroundcolor red
}

Output in PowerShell Window:

create site collection sharepoint online powershell
create site collection sharepoint online powershell

Our new Site Collection listed in Admin Center:

create site collection in sharepoint online using powershell
create site collection in sharepoint online using powershell

Newly Created Site Collection:

create subsite in sharepoint online using powershell
create subsite in sharepoint online using powershell

You may like following SharePoint PowerShell Tutorials:

Hope this helps, enjoy PowerShell with SharePoint Online and learn how to create a site collection using PowerShell in SharePoint Online Office 365..

Check out Best Alternative to InfoPath -> Try Now

EnjoySharePoint YouTube Channel

About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 13+ years, I worked in SharePoint 2007, 2010, 2013, 2016 and Office 365. I have extensive hands on experience in customizing SharePoint sites from end to end. Expertise in SharePoint migration tools like Sharegate, Doc Ave and Metalogix. Migrated SharePoint sites from SharePoint 2007 to 2010 and 2010 to 2013 several times seamlessly. Implementing CSOM with Microsoft best practices. Spent quality time in configuring SharePoint application services like User Profile, Search, Managed Meta data services etc. Now exploring SharePoint Framework and SharePoint 2019

View all posts by Krishna Vandanapu →