Create a Communication Site from code in SharePoint Online using PowerShell

Lately Microsoft released the new  and long awaited modern site template for publishing sites in SharePoint Online (Office 365). The site templates is named "Communication Site" and is the second template released after the modern "Team Site". This site template can be created if you have permissions from the SharePoint Home page using the "Create a site" form:

This approach is fine if you have permissions to create sites and are happy with the default setup. For my case this is almost never the case. Often the tenants I work with have restricted who can create new sites/groups, and also requires additional configuration after it has been created. This can be accomplished for the new "Team site" template, so I was curious how this can be done for the Communication site.

At the time of writing Microsoft have not released any documentation on how to do this, so this was done by re-creating the steps of the "Create a site" wizard (found by using Fiddler). I assume and hope that we sooner or later will find official documentation from Microsoft on how to do this. They might provide us with a bit more elegant way to use their API's. But if you are familiar with using general REST API's, this is pretty straight forward as soon you get authenticated with SharePoint.

UPDATE (30.10.2017): This documentation have now been released, and this method is now a supported approach to provision new Communication Sites.

PowerShell script for creating a Communication site

This script requires the "SharePoint Online Client Components" to be installed.

Summary

With the modern sites Microsoft has created several new REST based API's used in their own wizards. These API's can be used as long as you have a user authenticated context.

This code demonstrates how you can create the new Communication Site from code. After the site have been created you are free to connect to the site and apply customizations using in example the PnP PowerShell framework.