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?
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 "Team Site" option to create a modern team site collection with Office 365 group or "Other Options" to create a modern team site without Office 365 group.
    create modern site collection in sharepoint online
  • 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.
    sharepoint online create modern team site collection
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:
Team site is the most common type of site template used in SharePoint. As the name suggests, team sites are created for individual teams, departments, functional groups, etc. for team collaboration. You can create a modern site collection in SharePoint Online by specifying the site template as "STS#3". E.g.
#Connect to SharePoint Online
Connect-SPOService -url "https://crescenttech-admin.sharepoint.com" -Credential (Get-credential)
 
#Create a modern team site
New-SPOSite -Url "https://crescenttech.sharepoint.com/sites/Purchase" -Owner "[email protected]" -StorageQuota 2048 -Title "Purchase Team Site" -Template "STS#3"
The maximum number of site collections created on an Office 365 tenant is 500,000!

SharePoint Online: Create a Classic Site Collection
To create a classic site collection in SharePoint Online, do the following:
  • Go to the SharePoint admin center, Expand Sites >> Active Sites >> Click on the "Create" button in the toolbar. 
  • In the Create Site page, click on "Other options" and then click on the "Choose a template" drop-down and select "More Templates".
  • This takes you to the classic site collection creation page. Go ahead and enter your site collection details, Select the template as "Team site (classic experience)" and then click on the "OK" button once done.
    create classic team site sharepoint online powershell

Create a Classic Team Site in SharePoint Online using PowerShell
Similarly, To create classic site in SharePoint Online using PowerShell, Use: New-SPOSite cmdlet with template as "STS#0". Here is an example to create a classic team site:
#Connect to SharePoint Online
$Credential = Get-credential 
Connect-SPOService -url https://crescent-admin.sharepoint.com -Credential $Credential

#sharepoint online new site collection powershell
New-SPOSite -Url https://crescent.sharepoint.com/sites/marketing -Owner [email protected] -StorageQuota 1000 -Title "Marketing Team Site" -Template STS#0 
Wait for a minute and this creates a new site collection in SharePoint online.
create a site collection in sharepoint online using powershell

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 site collection
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 library 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://Crescenttech-admin.sharepoint.com/"
$NewSiteURL = "https://Crescenttech.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
#Define Variables
$AdminCenterURL = "https://crescenttech-Admin.sharepoint.com"
$SiteURL = "https://crescenttech.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
}
You can also use New-PnPSite cmdelet to create Modern Team sites and communication sites.

1 comment:

Please Login and comment to get your questions answered!

Powered by Blogger.