Tag Archives: O365

Creating SharePoint Site Collection through PowerShell CSOM

This is based on another blog post here: http://blog.scoreman.net/2013/02/create-site-collections-in-sharepoint-online-using-csom/. In that article the author shows how to use the CSOM with C# to create a Site Collection in Office 365.

This script is a near direct translation of that script into a PowerShell version of the code. I’ve also liberally taken inspiration from Chris O’Brien’s excellent series of posts on SharePoint PowerShell and CSOM here: http://www.sharepointnutsandbolts.com/2013/12/Using-CSOM-in-PowerShell-scripts-with-Office365.html

#Add the dlls required for working with Office 365
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.Online.SharePoint.Client.Tenant.dll"

#URLs and prerequisites
$adminSiteUrl = "<Admin URL>"
$newsiteUrl = "<URL of Site Collection to Create>"
$username = "<username"
$password = Read-Host "Please enter your Password" -AsSecureString

Write-Host "Establishing Connection to Office 365."
#Get the context and feed in the credentials
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($adminSiteUrl) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)  
$ctx.Credentials = $credentials 

Write-Host "Now configuring the new Site Collection"

#Get the tenant object
$tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($ctx)

#Set the Site Creation Properties values
$properties = New-Object Microsoft.Online.SharePoint.TenantAdministration.SiteCreationProperties
$properties.Url = $newsiteUrl
$properties.Template =  "STS#0"
$properties.Owner = $username
$properties.StorageMaximumLevel = 1000
$properties.UserCodeMaximumLevel = 300

#Create the site using the properties
$tenant.CreateSite($properties) | Out-Null

Write-Host "Creating site collection"
#Create the site in the tennancy
$ctx.ExecuteQuery()
Write-Host "Site Creation request completed. Note that the creation process is asynchronous and provisioning may take a short while."

I’ve tested this on Office 365 but haven’t tried it with On-Premise SharePoint 2013 so far.