Create Site Collection and Subsite in SharePoint Online using PowerShell

create subsite in sharepoint online using powershell
SharePoint deveopment training course

This PowerShell SharePoint tutorial, we will discuss how to create a site collection in SharePoint Online using PowerShell. Also, we will see how to create a subsite in SharePoint Online using PowerShell.

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.

To create a site collection in SharePoint, we need a site template id. For this, you can check my previous post on how to retrieve all site templates in SharePoint Online using PowerShell. We are going to use site template id while creating the site collection in SharePoint Online using PowerShell.

As a developer, you should have the below things required to create a site collection or subsites using PowerShell in SharePoint Online.

  • Basic knowledge in PowerShell
  • SharePoint Online Admin center 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 IDTemplate Name
STS#3Team site
STS#0Team site (classic experience)
BLOG#0Blog
BDR#0Document Center
DEV#0Developer Site
OFFILE#1Records Center
EHS#1Team Site – SharePoint Online configuration
BICenterSite#0Business Intelligence Center
SRCHCEN#0Enterprise Search Center
BLANKINTERNETCONTAINER#0Publishing Portal
ENTERWIKI#0Enterprise Wiki
PROJECTSITE#0Project Site
PRODUCTCATALOG#0Product Catalog
COMMUNITY#0Community Site
COMMUNITYPORTAL#0Community Portal
SRCHCENTERLITE#0Basic Search Center
visprus#0Visio Process Repository
SRCHCENTERLITE#0Basic Search Center
visprus#0Visio Process Repository

Create site collection SharePoint Online using PowerShell

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 using powershell

Our new Site Collection listed in Admin Center:

create site collection sharepoint online

You can see below the newly created modern site collection in SharePoint Online.

create site collection sharepoint online powershell

This is how we can use PowerShell, to create a modern site collection in SharePoint Online Office 365.

Create Subsite in SharePoint Online using PowerShell

Now, we will see how to create a subsite in SharePoint Online using PowerShell.

It is always a good idea to create subsites to organize user content for better collaboration across the organization thought it is SharePoint online.

The below PowerShell script will create a subsite in SharePoint online:

function new-spOnlineWeb {

#variables that needs to be set before starting the script
$siteURL = "<< SharePoint Site Collection URL >>"
$webURL = "MyFirstWeb"
$title = "My First Web"
$template = "STS#0"
$userName = "<< User-ID >>"
$useSamePermissionsAsParentSite = $true

# Let the user fill in their password in the PowerShell window
$password = Read-Host "Please enter the password for $($userName)" -AsSecureString

# set SharePoint Online credentials
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)

#Creating client context object
$context = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$context.credentials = $SPOCredentials

#create web using WebCreationInformatin object (wci)
$wci = New-Object Microsoft.SharePoint.Client.WebCreationInformation
$wci.url = $webURL
$wci.title = $title
$wci.webtemplate = $template
$wci.useSamePermissionsAsParentSite = $useSamePermissionsAsParentSite
$createWeb = $context.web.webs.add($wci)
$context.load($createWeb)

#send the request containing all operations to the server
try{
$context.executeQuery()
write-host "info: Creating $($title)" -foregroundcolor green
}
catch{
write-host "info: $($_.Exception.Message)" -foregroundcolor red
}
}

new-spOnlineWeb

Below is the expected output:

create modern site collection sharepoint online powershell
create modern subsite collection sharepoint online powershell

You may like following SharePoint site collection tutorials:

This SharePoint tutorial, we learned how to create a site collection in SharePoint Online using Office 365. We also saw, how to create a subsite in SharePoint Online Office 365 using PowerShell.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

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 →