kwizcom banner advertisement

SharePoint Online: How to Create a Site Collection using PowerShell?

Requirement: Create a site collection in SharePoint Online using PowerShell!

How to Create a Site Collection in SharePoint Online?
Site collection in SharePoint is a logical group sites with a common top-level site and subsites organized as a hierarchy. Every site collection share common top site, navigation, security, content types, etc. To create a SharePoint Online Site collection from Browser, 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" >> and then "Private Site collection" to create a new site collection.
  • 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.

SharePoint Online: PowerShell to Create Site Collection
To create site collections in SharePoint Online using PowerShell, Use: New-SPOSite cmdlet. Here is an example:
#Connect to SharePoint Online
$Credential = Get-credential 
Connect-SPOService -url https://crescent-admin.sharepoint.com -Credential $Credential

#Create new Site Collection
New-SPOSite -Url https://crescent.sharepoint.com/sites/marketing -Owner salaudeen@crescent.com -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

SharePoint Online: Create Modern Team Site Collection using PowerShell:
Similarly, 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 "Salaudeen@TheCrescentTech.com" -StorageQuota 2048 -Title "Purchase Team Site" -Template "STS#3"
Now, lets add some error handling, and wrap it inside a re-usable function:

Create SharePoint Online Site Collection using PowerShell:
Lets add some error handling and make the script bit more flexible.. 
#Function to Create Site Collection
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 = "SpAdmin@Crescent.com"
$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
    {
        #create the site collection
        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 = "spsadmin@Crescent.com"
$StorageQuota = 1000
$ResourceQuota = 300
$SiteTemplate = "STS#0"

#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 SharePoint Online Admin Center.

Create New Site Collection in SharePoint Online using CSOM
#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
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
     
    #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 = "salaudeen@theCrescenttech.com"
    $SiteCreationProperties.StorageMaximumLevel = 1000
    $SiteCreationProperties.UserCodeMaximumLevel = 300
 
    #Create the site using the properties
    $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
#Define Variables
$AdminCenterURL = "https://crescenttech-Admin.sharepoint.com"
$SiteURL = "https://crescenttech.sharepoint.com/sites/procurement2"
$SiteTitle = "Crescent Procurement Portal"
$SiteOwner = "Salaudeen@TheCrescentTech.com"
$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)
    {
        #Create site collection based on given parameters
        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.
SharePoint Online: How to Create a Site Collection using PowerShell? SharePoint Online: How to Create a Site Collection using PowerShell? Reviewed by Salaudeen Rajack on June 04, 2016 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.