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 https://www.microsoft.com/en-us/download/details.aspx?id=35588.

image

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 https://technet.microsoft.com/en-us/library/fp161370.aspx and the template name using part 1

function new-SPOnlineSite {
# variables that needs to be set before starting the script
$url = "https://spfire.sharepoint.com/sites/BlogDemo"
$title = "Blog Demo"
$owner = "mpadmin@spfire.onmicrosoft.com"
$storageQuota = 1000
$resourceQuota = 50
$template = "STS#0"
$adminUrl = "https://spfire-admin.sharepoint.com"
$userName = "mpadmin@spfire.onmicrosoft.com"

# 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
try{
Connect-SPOService -Url $adminUrl -Credential $credentials
write-host "Info: Connected succesfully to Office 365" -foregroundcolor green
}
catch{
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
}
else{
write-host "info: $($url) still exists in the recyclebin" -foregroundcolor red
}
}
new-SPOnlineSite

image

You will be asked to enter the password and press enter

image

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

image

Leave a Reply

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