How to create site collections by PowerShell using SharePoint Online Management Shell

Let’s say you are migrating from another system and you need to create multiple site collection by PowerShell?

This code covers :

  1. How to connect to office 365 using credentials stored in a text file, so that you don’t have to re-authenticate each time you run it.
  2. Iterate an array of sites URL to be created
  3. Create team sites (template sts). The template id has been found with Get-SPOSite on an existing website. You can create a site manually and find its template this way

1. Connect to Office

This is the part that shall be done once per tenant, to save the account password as a text file.

Read-Host -Prompt "" -AsSecureString | ConvertFrom-SecureString | Out-File "C:\DEV\tenantcred.txt"

Once this done, call the helper with your tenant name (adminUPN), it will build the admin URL automatically (https://tenant-admin.sharepoint.com):

ConnectToO365 $passwordCredAsText $adminUPN $orgName

2. Create the site collections

To know which template you shall create, you could create one manually and get it using Get-SPOSite with the -detailed fl (full list parameter)

#Get-SPOSite -Identity https://mytenant.sharepoint.com/sites/site1 -detailed |fl

Then create your site collection:

New-SPOSite -Url $url -Template "STS#0" -Owner "domain\\account1" -StorageQuota
"26214400"

3. The full code (create this file as createSites.ps1)

$orgName = "mytenant"
$adminUPN = "myaccount@tenant.com"
 
$passwordCredAsText = "C:\DEV\myCred.txt"
. ./ConnectToAdminO365Admin.ps1
ConnectToO365 $passwordCredAsText $adminUPN $orgName
 
#view a site information
#Get-SPOSite -Identity https://mytenant.sharepoint.com/sites/site1
 
#do not put a "/" at the end of the url
$arrayOfUrl = @("http://mytenant.sharepoint.com/sites/site2",
"https://mytenant.sharepoint.com/sites/site3", "https://mytenant.sharepoint.com/sites/site4")
 
foreach ($url in $arrayOfUrl) {
Write-Host "Creating $url"
New-SPOSite -Url $url -Template "STS#0" -Owner "domain\\account1" -StorageQuota "26214400"
#"https://$orgName.sharepoint.com/sites/$siteUrl"
}
 
#show site information such as site template : STS#0
#Get-SPOSite -Identity https://mytenant.sharepoint.com/sites/qed -detailed |fl

The helper (create this as a second file)

ESPC call for speakers 2024
Create a file named :

ConnectToAdminO365Admin.ps1

###############################
#.SYNOPSIS
###############################
# By JEFF ANGAMA 19 02 2018 - https://twitter.com/jeffangama
# This code shall be tested before ran onto production
#.DESCRIPTION
#Long description
# Before connecting to O365, this function allow you to save your password securely so that you dont have to retype it in future.
# Use the command below, rename the text file to suit your tenant
# Then run this function, see the example section for example
# COMMAND to store your credentials
# dev
#.PARAMETER sInputFile
#Parameter description
#
#.PARAMETER adminEmail
#Parameter description
#
#.PARAMETER orgName
#Parameter description
# What is the o365 org name ? its the tenant name, to build the admin url https://$orgName-admin.sharepoint.com
#.EXAMPLE
#An example
#
#$orgName = "mytenant"
#$adminUPN = "account@domain.com"
#$passwordCredAsText = "C:\DEVMIS\myTenantCred.txt"
#. ./../Common/PS/O365/ConnectToAdminO365Admin.ps1
#ConnectToO365 $passwordCredAsText $adminUPN $orgName
#.NOTES
#General notes
##############################
function ConnectToO365 {
param ($sInputFile, $adminEmail, $orgName)
Write-Host -ForeGroundColor Green "Ensure you have stored your password in text file, refer to the help in this function"
 
#run this command once
#Read-Host -Prompt "" -AsSecureString | ConvertFrom-SecureString | Out-File "C:\DEV\tenantcred.txt"
$orgName = $orgName
$tenantAdminURL = "https://$orgName-admin.sharepoint.com"
$Pass = Get-Content $sInputFile | ConvertTo-SecureString
$userCredential = new-object -typename System.Management.Automation.PSCredential($adminEmail, $Pass)
Write-Host "Connecting Connect-SPOService -Url $tenantAdminURL -Credential $userCredential"
 
Connect-SPOService -Url $tenantAdminURL -Credential $userCredential
 
Write-Host "Connected to $tenantAdminURL with $adminEmail"
}

What we’ve seen

We have seen how to create a script that creates multiple site collections and how to connect to office 365 quickly without retyping credentials

 

Reference:
Angama, j. (2018). How to create site collections by PowerShell using SharePoint Online Management Shell. [online] Jeff ANGAMA OFFICE 365 NOTES. Available at: https://jeffangama.wordpress.com/2018/02/19/how-to-create-site-collections-by-powershell-using-sharepoint-online-management-shell/ [Accessed 20 Feb. 2018].

Share this on...

Rate this Post:

Share: