Saturday, June 4, 2016

SharePoint Online: How to Create New Site Collection using PowerShell

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

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.
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:
#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 site collection will be in a minute! You can verify by going to SharePoint 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.
*Sponsored


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...