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.

5 thoughts on “Creating SharePoint Site Collection through PowerShell CSOM

  1. Tobias West

    Hi,

    Great script, a useful addition to this script would be to wait for the site creation rather than give your message. Here’s how I did it (from line 31 of your script)

    #retrieve the SPO Operation return value during creation
    $spOnlineOperation = $tenant.CreateSite($properties)

    #load the tenant object
    $ctx.Load($tenant)
    #load the SPO operation
    $ctx.Load($spOnlineOperation)
    #run the command
    $ctx.ExecuteQuery()

    #wait for SPO Operation to complete
    while($spOnlineOperation.IsComplete -eq $false)
    {
    write-host “Waiting…” -ForegroundColor Yellow
    Start-Sleep 10
    $spOnlineOperation.RefreshLoad()
    $tenantCtx.ExecuteQuery()
    }

    write-host “Site Created”

    (Tested on O365 only)

    Reply
  2. Biraj Bhalodia

    Can we set owner as security group for sharepoint online (o365) site?
    I am trying to set directly as GroupName and also with security group email address but getting error like not a valid user

    Reply
    1. Alex Post author

      I don’t believe so.

      In on-premises SharePoint you cannot set a group to be the ‘Owner’ of a site, although they can be added to a group that has full control. O365 is based on the same code base and I suspect the same restriction is in place there.

      Reply
  3. Arpeet

    Hello,

    I am getting an error but can’t find out the reason:
    Line of code: $tenant = New-Object Microsoft.Online.SharePoint.TenantAdministration.Tenant($Context)
    Error: A positional parameter cannot be found that accepts argument ‘A positional parameter cannot be found that accepts argument ‘Cannot find an
    overload for “Tenant” and the argument count: “1”.’.’.

    Any reason?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *