Monday, March 2, 2015

Setting up Powershell to Work With SharePoint o365

Hey, automation is the way to go. At my day job we do a lot of work with Powershell scripts. Attempting the same with SPo365 is pretty limited... at least the standard OOTB API is.

Problem: You can't do much

Solution: Use the SharePoint Client API! Just load it into Powershell and away you go. Pretty slick that you can use your knowledge of the client api in Powershell.


  1. Get your hands on the SharePoint Client Components installer. Install it.
  2. It should install to the default location at "". If not make changes to the code below.
  3. Insert this at the top of your script

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\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"


* If you can't run scripts, try running as admin and maybe set your Powershell policy to "Set-Executionpolicy -ExecutionPolicy Unrestricted"

Here's a sample script to create a sub-site:




$siteUrl = "https://YOUR SPO365 URL HERE"

$password = Read-Host -Prompt "Enter password" -AsSecureString

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("Your@email.here", $password)

$ctx.Credentials = $credentials

$webCreationInformation = New-Object Microsoft.SharePoint.Client.WebCreationInformation

$webCreationInformation.Url = "demo01site"

$webCreationInformation.Title = "Demo 01 Site"

$webCreationInformation.WebTemplate = "PROJECTSITE#0"

$newWeb = $ctx.Web.Webs.Add($webCreationInformation)

$ctx.Load($newWeb)

$ctx.ExecuteQuery()


Write-Host "Title" $newWeb.Url " Created"

No comments:

Post a Comment