Use PowerShell to Interact with Office 365

One of my biggest frustration with Office 365 over the past few months, is the fact that we are now prohibited from creating new subsites under the main Internet Public facing site collection that is given to us by SharePoint online. Using SharePoint Online Management Shell only gives us 30 PowerShell cmdlets to use, and none of them allow us to create new SPWeb objects. The only option left to create a subsite on an Office 365 ​public site is to write custom code, but custom code also means that this can be archived using PowerShell. Remember that PowerShell is able to leverage all of the .NET framework components. We can then use the .NET Managed Client-Side Object Model (CSOM) of SharePoint and make remote calls to the Office 365 SharePoint Online object model. The following PowerShell script will prompt you to enter the URL of your SharePoint Online public site collection (using the https:// prefix), the credentials for your account, as well as the name and relative URL for this new Web you are trying to create (see Figure 1 for a real-life example).


Figure 1 – Creating a new Office 365 SharePoint online Web using a custom PowerShell script

Full PowerShell Script:


$url = Read-Host -Prompt “Site Collection URL”
$username = Read-Host -Prompt “Username” 
$password = Read-Host -Prompt “Password” -AsSecureString
$webTitle = Read-Host -Prompt “Name of new Web”
$webUrl = Read-Host -Prompt “Relative URL of new Web”
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$webInfo = New-Object Microsoft.SharePoint.Client.WebCreationInformation
$webInfo.Title = $webTitle
$webInfo.Url = $webUrl
$webInfo.WebTemplate = “STS#0”
$webInfo.Language = 1033
$newWeb = $ctx.Web.Webs.Add($webInfo)

Leave a Reply

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