Create Site Collection in SharePoint online using PowerShell

create subsite in sharepoint online using powershell
InfoPath alternatives for form designing SharePoint

Objective:
In my previous article I have discussed about various site templates available in SharePoint Online. Today I would like to walk you through the process of provisioning Site Collection in SharePoint Online using PowerShell.

Prerequisites:
What we need to achieve this scenario is:
1. Basic knowledge in PowerShell
2. SPO Admin URL & Access
3. New Site Collection URL & Owner name
4. Site collection template ID

Read SharePoint 2013 tutorials

List of Site Collection templates in SharePoint Online:

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:

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

Hope this helps, enjoy PowerShell with SharePoint Online!!!

Similar SharePoint 2013 Tutorials

About Krishna Vandanapu

I am Krishna.Vandanapu a SharePoint architect working in IT from last 12 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 →

Leave a Reply