Saturday, June 4, 2016

SharePoint Online: How to Create New 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/or zero or more 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 SharePoint Admin Center as a tenant admin or SharePoint Online Administrator. 
  • Click on "Site Collections" from left navigation >> Click on "New" >> and then "Private Site collection" to create a new site collection.
  • The New Site Collection page appears. Set properties according to your requirement to create site collection. Such as: 
    • Title and description for your new site, 
    • Site Address (URL path) for the site
    • Select the Template for your site collection, typically "Team Site" will do. 
    • Set the storage quota, Site collection Primary owner for the site collection. 
    • Click OK to start creating new site collection. Wait for a moment and your site collection should appear in the site collections list.
sharepoint online create site collection

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 -Credential $Credential

#Create new Site Collection
New-SPOSite -Url -Owner -StorageQuota 1000 -Title "Marketing Team Site" -Template STS#0 
Wait for a minute and this creates a new site collection in SharePoint online.
sharepoint online powershell create site collection
Now, lets add some error handling, and wrap it inside a re-usable function:

SharePoint online PowerShell to create site:
Lets add some error handling and make the script bit more flexible.. 
#Function to Create Site Collection
Function Create-SPOSite 
        [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 = ""
$AdminName = ""
$AdminPassword="Password Goes here"
$SecurePassword = $AdminPassword | ConvertTo-SecureString -AsPlainText -Force 
$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $AdminName, $SecurePassword
    #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
        #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= ""
$SiteOwner = ""
$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 Site Collections in Bulk from a CSV File:
If you want to create site collections in bulk, you can use the above function with a CSV file.
  • Step 1: Create a CSV file in the below format. Populate the column values according to your requirements.
    powershell to create site collections from csv in sharepoint online
  • Step 2: Use this PowerShell script to read from CSV and create site collection using the function created above.
    #Read from CSV and create site collection
    Import-Csv "C:\SitesToCreate.csv" | Foreach-Object { 
       Create-SPOSite -Title $_.SiteTitle -URL $_.SiteURL -Owner $_.SiteOwner -StorageQuota $_.StorageQuota -ResourceQuota $_.ResourceQuota -Template $_.SiteTemplate  }

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...