In 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 get 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
SharePoint Tutorial Contents
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 |
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:
Our new Site Collection listed in Admin Center:
You can see below the newly created modern site collection in SharePoint Online.
This is how we can use PowerShell, to create a modern site collection in SharePoint Online Office 365.
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:
You may like following SharePoint site collection tutorials:
- Get all users and groups in SharePoint 2013/2016 using PowerShell
- Self-Service Site Collection in SharePoint 2013/2016
- Bulk SharePoint Online Site Collection Creation using PowerShell
- SharePoint 2016 Fast Site Collection Creation using PowerShell
- How to make SharePoint 2013 site collection read-only and restrict delete?
- How to Change Site Collection Administrators in SharePoint 2013?
- SharePoint 2016 Create a web application and a site collection using PowerShell
- SharePoint 2013/2016: Get all ListTemplates from Site collection using PowerShell
- Backup and Restore Site collection in SharePoint 2016/2013/2010/2007
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.
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