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

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.

[code language=”powershell”]
Read-Host -Prompt “” -AsSecureString | ConvertFrom-SecureString | Out-File “C:\DEV\tenantcred.txt”
[/code]
Once this done, call the helper with your tenant name (adminUPN), it will build the admin URL automatically (https://tenant-admin.sharepoint.com):

[code language=”powershell”]
ConnectToO365 $passwordCredAsText $adminUPN $orgName
[/code]

3. 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)

[code language=”powershell”]
#Get-SPOSite -Identity https://mytenant.sharepoint.com/sites/site1 -detailed |fl
[/code]
Then create your site collection :

[code language=”powershell”]
New-SPOSite -Url $url -Template “STS#0” -Owner “domain\\account1” -StorageQuota
“26214400”
[/code]

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

[code language=”powershell”]
$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
[/code]

The helper (create this as a second file)

Create a file named :

ConnectToAdminO365Admin.ps1

[code language=”powershell”]
###############################
#.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”
}
[/code]

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

References

- Advertisement -

1 COMMENT

  1. […] How to create site collections by PowerShell using SharePoint Online Management Shell (Sean Gerlinger’s SharePoint blog site) If you are trying to configure SharePoint Add-ins or Apps on a farm that has multiple web applications using SSL encryption. It can be a bit of challenge to understand how to configure the App domain to work with SSL. Now that we can use Server Name Indication (SNI) since IIS 8.0. We can have multiple SSL certificates bound to one port in IIS for each IIS site. No need to have multiple IP addresses on the web front end servers anymore. Below I go through each configuration step briefly to help you understand how to set a SharePoint farm to use Add-in or Apps. […]

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CONNECT FOR MORE CONTENT

DO NOT MISS THOSE ARTICLES

Recent Comments