Basic PnP Powershell SharePoint Site Collection creation script


At a customer we recently created a new script to create project sites. Here is a simplified version of the code, mainly for myself for re-use in the future.

Let me start with a disclaimer: I am sure the code could be improved a lot, but I like the simplicity and readability of it. If you don’t know PowerShell, or if you are not a developer (like me!!!), this will do the trick and lets you learn some basic PnP Powershell. Next to that, it is a fun way to start learning PnP PowerShell.


What is PnP PowerShell

You probably already heard about PowerShell? It is a scripting language that lets you do all kinds of work on a number of things, including Office 365 and SharePoint (on prem and online).

PnP (which stands for Patterns and Practices) is a specific library with commands that are just super easy to read and write. Really, since I discovered this set of commands, the SharePoint admin part of my life has become so much easier and more pleasant!

Read the official info, with all functions here!


Why should I use this to provision new SharePoint sites?

Well, frankly.. if you still want to create SharePoint-only sites (so no groups) there aren’t a lot of ways how you can do it these days. From the admin center is one way, but if you have more sites you need to be created, then this way will save you some time.

So in the case of my customer, who creates around 300 project sites per year, creating this script once and just letting it run everytime we need a new site has a lot of impact on my time and correctness of the sites.

The difficulty in creating a new SharePoint site collection, is that could take from 2 minutes to 2 hours before it is actually created. That is why you need to solve the waiting time.. The way we do it in this script is a loop that does a 30 second check to see if the site collection is available. It will end the loop when the Site Collection is there.

The code

Copy/paste this code into your favorite PowerShell editor (like PowerShell ISE or Visual Studio Code or whatever you like). The code will ask you

After that, it will start creating a SharePoint site collection, based on a Team Site template.

In the beginning of the script, there are some settings that you must (!!!) and you can adapt to set the site to something more useful for yourself.


# you need to modify these settings !!
$AdminUrl = ""
$SharePointMainurl = ""
$SiteOwner = ""

# you can modify these settings
$SiteTemplate = "STS#0"
$StorageQuota = "5120"
$Timezone = "4"

#Connecting to your environment
Connect-PnPOnline -Url $AdminUrl -Scopes "Group.ReadWrite.All","User.Read.All"
write-host "Connected with PnPOnline"

#Getting the information we need to create the site
$groupname = read-host -Prompt "Please enter the url of the new SharePoint site"
$sitename = read-host -Prompt "Please enter the name of the new SharePoint site"

write-host "Creating " $sitename

#The line of code that creates the new site collection
New-PnPTenantSite -Title $sitename -Url "$SharePointMainurl/$groupname" -Owner $SiteOwner -TimeZone $Timezone -Template $SiteTemplate -StorageQuota $StorageQuota

# Site creation wait code
write-host "Giving SharePoint some time to start creating the site"
Start-sleep -s 30
$sitecreationtimeoutminutes = 60
$sitecreationstart = get-date
$status = (Get-PnPTenantSite -Url "$SharePointMainurl/$groupname").status
while ($status -ne 'Active') {
$ts = New-timespan -start $sitecreationstart -End (get-date)
If ($ts.Minutes -gt $sitecreationtimeoutminutes) {
Error "Site could't be created within the configured maximum timespan, giving up."
Start-sleep -s 30
$status = (Get-PnPTenantSite -Url "$SharePointMainurl/$groupname").status
Write-Host "status = $status"
Write-Host "Your new SharePoint site $sitename is created at this link: $SharePointMainurl/$groupname"

About: Marijn

Marijn Somers (MVP) has over 14 years experience in the SharePoint world, starting out with SP2007. Over the years the focus has grown to Office 365, with a focus on collaboration and document management. He is a business consultant at Balestra and Principal Content Provider for "Mijn 365 Coach" that offers dutch employee video training. His main work tracks are around user adoption, training and coaching and governance. He is also not afraid to dig deeper in the technicalities with PowerShell, adaptive cards or custom formatting in lists and libraries. You can listen to him on the biweekly "Office 365 Distilled" podcast.