Create new site in SharePoint Online with PowerShell

This is part 2 of 10 where we will be creating a new site in SharePoint Online with PowerShell. This is part of the following series:

We will be collecting all available web templates in part 1 so we can use this to create a new site in part 2. In part 3 we will be creating a web for the newly created site. We then want to create a couple of site columns in part 4 which we will combine to a content type in part 5. This content type will be added (part 8) to our newly created document library in part 7 using a list template from part 6. After everything is set we will be setting the view in part 9 for this list to show the added columns we got from adding the content type. We only want to set permissions for myself so I’ll will be breaking the inheritance and setting permissions in part 10.

Create new site in SharePoint Online with PowerShell

This script will create a new site in SharePoint Online based on the specified variables. This script will be not be using CSOM as we have an SPO command to create a new site. We will first start by opening the SharePoint Online Management Shell as administrator which can be downloaded at


You will need to change the first variables to match your Office 365 tenant and copy this bit to PowerShell. You can find all available parameters for New-SPOSite at and the template name using part 1

function new-SPOnlineSite {
# variables that needs to be set before starting the script
$url = ""
$title = "Blog Demo"
$owner = ""
$storageQuota = 1000
$resourceQuota = 50
$template = "STS#0"
$adminUrl = ""
$userName = ""

# Let the user fill in their password in the PowerShell window
$password = Read-Host "Please enter the password for $($userName)" -AsSecureString

# Set credentials
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $password

# Connect to to Office 365
Connect-SPOService -Url $adminUrl -Credential $credentials
write-host "Info: Connected succesfully to Office 365" -foregroundcolor green
write-host "Error: Could not connect to Office 365" -foregroundcolor red
Break new-SPOnlineSite

#verify if site already exists in SharePoint Online
$siteExists = get-SPOSite | where{$_.url -eq $url}

#verify if site already exists in the recycle bin
$siteExistsInRecycleBin = get-SPODeletedSite | where{$_.url -eq $url}

#create site if it doesn't exists
if (($siteExists -eq $null) -and ($siteExistsInRecycleBin -eq $null)) {
write-host "info: Creating $($title)" -foregroundcolor green
New-SPOSite -Url $url -title $title -Owner $owner -StorageQuota $storageQuota -NoWait -ResourceQuota $resourceQuota -Template $template
elseif ($siteExists -eq $true){
write-host "info: $($url) already exists" -foregroundcolor red
write-host "info: $($url) still exists in the recyclebin" -foregroundcolor red


You will be asked to enter the password and press enter


The site will now be created in SharePoint Online so navigate to the SharePoint Admin Center to verify that the site has been created


get site, web and list information with PowerShell for SharePoint Online

We often get the question to list the site structure for SharePoint on premise and this can be done quite easily with get-spsite, get-spweb and get-splist but only get-sposite is available for SharePoint Online. This script will do the following:

– get all root sites in SharePoint Online and show its template

– get all webs and subwebs from root sites in SharePoint Online and show Its template

– get all lists from sites and webs in SharePoint Online and show its item count

– collect this information and store this in an HTML table

The script will generate the following output:


You will need to install the SharePoint Online Management Shell from and all of its pre-requisites. The below script needs to be copied to a .ps1 file on the workstation you are planning to run this script. It will create a logfile on the same location.

function connectToO365{

# Let the user fill in their admin url, username and password

$adminUrl = Read-Host "Enter the Admin URL of 0365 (eg. https://<Tenant Name>"

$userName = Read-Host "Enter the username of 0365 (eg. admin@<tenantName>"

$password = Read-Host "Please enter the password for $($userName)" -AsSecureString

# set credentials

$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $password

$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)

#connect to to Office 365


Connect-SPOService -Url $adminUrl -Credential $credentials

write-host "Info: Connected succesfully to Office 365" -foregroundcolor green



write-host "Error: Could not connect to Office 365" -foregroundcolor red

Break connectToO365


#create HTML file

$filePath = create-outputfile

#start getting site collections


add-content -value "



" -path $filePath


function create-outputfile(){

#Create unique string from the date

$date = get-date -format dMMyyyyhhmm

#set the full path

$filePath = "$($PSScriptRoot)\Output$($date).html"

#test path

if (!(Test-Path -path $filePath)){

#create file

New-Item $filePath -type file | out-null

#print info

write-host “File created: $($filePath)” -foregroundcolor green

#add start HTML information to file

add-content -value "



<h1>Sites information Office 365</h1>

<table border='1' style='font-family: Calibri, sans-serif'>


<th style='background-color:blue; color:white'>URL</th>

<th style='background-color:blue; color:white'>Type</th>

<th style='background-color:blue; color:white'>Template</th>

<th style='background-color:blue; color:white'>Item Count</th>


" -path $filePath



#break so there won't be duplicate files

write-host "Output file already exists, wait 1 minute" -foregroundcolor yellow

Break create-outputfile


return $filePath


function get-siteCollections{

#Get all site collections

$siteCollections = Get-SPOSite

#loop through all site collections

foreach ($siteCollection in $siteCollections)


#set variable for a tab in the table

$pixelsweb = 0

$pixelslist = 0

#add info to HTML document

add-content -value "<tr style='background-color:cyan'><td>$($siteCollection.url)</td><td>TopSite</td><td>$($sitecollection.template)</td><td></td></tr>" -path $filePath

write-host "Info: Found $($siteCollection.url)" -foregroundcolor green

#search for webs

$AllWebs = Get-SPOWebs($siteCollection.url)



function Get-SPOWebs($url){

#fill metadata information to the client context variable

$context = New-Object Microsoft.SharePoint.Client.ClientContext($url)

$context.Credentials = $SPOcredentials

$web = $context.Web






#loop through all lists in the web

foreach($list in $web.lists){

add-content -value "<tr><td><span style='margin-left:$($pixelslist)px'>$($list.title)</td><td>List/library</td><td></td><td>$($list.itemcount)</td></tr>" -path $filePath


#loop through all webs in the web and start again to find more subwebs

$pixelsweb = $pixelsweb + 15

$pixelslist = $pixelslist + 15

foreach($web in $web.Webs) {

add-content -value "<tr style='background-color:yellow'><td><span style='margin-left:$($pixelsweb)px'>$($web.url)</td><td>Web</td><td>$($web.webtemplate)</td><td></td></tr>" -path $filePath

write-host "Info: Found $($web.url)" -foregroundcolor green





write-host "Could not find web" -foregroundcolor red






This script has been run on a workstation with Windows 8.1. First run the SharePoint Online Management Shell as administrator

SharePoint Online Management Shell logo.

Then run the following command

& “script location”


And answer the following three questions


And press enter


All sites have been successfully crawled and its contents is listed in the HTML document located next to the .ps1 file


You can list more information if you want and you can find the properties on the following locations:

– SPOSite properties:

– Web properties:

– List properties:

Let me know if you have any remarks or updates

Unlock SharePoint Site Collections

You can lock or unlock SharePoint Site Collections manually but it’s also possible your Site Collection gets locked by SharePoint. A Site Collection backup for example locks the Site Collection during backup and this lock will remain in place if the backup could not be completed. Your Site Collection can also be in Maintenance Mode which makes it unable to set the state to “Not Locked” without PowerShell.

Which Site Collections are locked?

You can run the following script to find out in which state your Site Collection is

#Add PowerShell Snapin
Add-pssnapin Microsoft.SharePoint.Powershell -ErrorAction silentlycontinue

#Get all Site Collections
$SiteCollections = get-spsite -limit all

#Loop through all site collections
foreach($SiteCollection in $SiteCollections)
#Remove "SPSite Url=" from variable
$Url = $SiteCollection.url.Replace("SPSSite Url=","")

#Verify the properties from the Site Collection
if ($SiteCollection.ReadOnly -eq $false -and $SiteCollection.ReadLocked -eq $false -and $SiteCollection.WriteLocked -eq $false)
write-host "$Url : Unlocked" -foregroundcolor Green
elseif ($SiteCollection.ReadOnly -eq $false -and $SiteCollection.ReadLocked -eq $false -and $SiteCollection.WriteLocked -eq $true)
write-host "$Url : Adding content prevented" -foregroundcolor yellow
elseif ($SiteCollection.ReadOnly -eq $true -and $SiteCollection.ReadLocked -eq $false -and $SiteCollection.WriteLocked -eq $true)
write-host "$Url : Read-only" -foregroundcolor Red
elseif ($SiteCollection.ReadOnly -eq $null -and $SiteCollection.ReadLocked -eq $null -and $SiteCollection.WriteLocked -eq $null)
write-host "$Url : No access" -foregroundcolor yellow
elseif ($SiteCollection.MaintenanceMode -eq $true)
write-host "$Url : Maintenance Mode" -foregroundcolor red



Unlocking the Site Collection

By Central Administration

1. Open Central Administration

2. Click on ‘Application Management’

3. Click on ‘Configure quotas and locks’

4. Navigate to the locked site collection


5. Unlock the Site Collection


6. Click on OK

By PowerShell

Run the following script to unlock a single Site Collection or add this line to the above script to unlock the site collection immediately.

Set-SPSite -Identity “<SiteCollection>” -LockState Unlock


Maintenance Mode

The following warning is viewable on the site when the Site Collection is in maintenance mode


and you are unable to modify the lock manually or with the normal PowerShell commands.


You can only disable maintenance mode for a Site Collection with PowerShell by running the following commands

$Admin =  new-object Microsoft.SharePoint.Administration.SPSiteAdministration(‘<SiteCollectionURL>’)