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:
- Login to New SharePoint Admin Center as a tenant admin or SharePoint Online Administrator.
- Click on Sites >> Active Sites from left navigation >> Click on “Create”
- 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.
- Pick the site template such as “Team site”, provide the site Name, URL, Administrator options and click on “Finish” to create a site collection in SharePoint Online.
Wait for a moment and your site collection should appear in the site collections list. Creating a site collection from the SharePoint admin center 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 "[email protected]" -StorageQuota 2048 -Title "Purchase Team Site" -Template "STS#3"
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 = "[email protected]"
$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 = "[email protected]"
$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"
$SiteCreationProperties.Owner = "[email protected]"
$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
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/procurement2"
$SiteTitle = "Crescent Procurement Portal"
$SiteOwner = "[email protected]"
$Template = "STS#3" #Modern 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 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
Great post