Creation of SharePoint Online sites from CSV
This script helps you in creation of SharePoint Online Communication and Team sites in bulk. It takes an input from CSV.
Summary
This script helps you in creation of SharePoint Online Communication and Team sites in bulk. It takes an input from CSV. CSV structure should look like:
| RootSiteUrl | SiteTitle | Alias | SiteUrl | Type |
|---|---|---|---|---|
| https://.sharepoint.com | My Communication Site | /sites/mycommsite | CommunicationSite | |
| https://.sharepoint.com | My Team Site | MyTeamSite | /sites/myteamsite | TeamSite |
$adminCenterUrl = "https://<tenant_name>-admin.sharepoint.com/"
$inputFilePath = "D:\dtemp\sites.csv"
$inputSites = import-csv $inputFilePath
Write-Host "Invoked CSV file"
# Connect to SharePoint Admin Center
Connect-PnPOnline -Url $adminCenterUrl -Interactive
foreach($row in $inputSites)
{
Write-Host "Getting configuration details from CSV"
$RootSiteUrl = $row.RootSiteUrl
$SiteTitle = $row.SiteTitle
$SiteUrl = $row.SiteUrl
$Type = $row.Type
$Alias = $row.Alias
$commSiteUrl = $RootSiteUrl + $SiteUrl
$site = $null
try
{
$site = Get-PnPTenantSite -Identity $commSiteUrl
}
catch
{
Write-Host "Exception: $($_.Exception.Message)"
}
If ($null -eq $site)
{
if($Type -eq "TeamSite")
{
# Creating a SharePoint team site
$ProvisionedSiteUrl = New-PnPSite -Type $Type -Title $SiteTitle -Alias $Alias -IsPublic
Write-Host "Site Collection $($ProvisionedSiteUrl) Created Successfully!" -foregroundcolor Green
}
else
{
# Creating a SharePoint communication site
$ProvisionedSiteUrl = New-PnPSite -Type $Type -Title $SiteTitle -Url $commSiteUrl
Write-Host "Site Collection $($ProvisionedSiteUrl) Created Successfully!" -foregroundcolor Green
}
}
else
{
Write-Host "Site Collection $($SiteUrl) exists already!" -foregroundcolor Yellow
$ProvisionedSiteUrl = $SiteUrl;
}
}
# Disconnect SharePoint online connection
Disconnect-PnPOnline
Write-Host "Script execution completed successfully!" -foregroundcolor Green
$inputFilePath = "D:\dtemp\sites.csv"
$inputSites = import-csv $inputFilePath
Write-Host "Invoked CSV file"
# Get Credentials to connect
$m365Status = m365 status
if ($m365Status -match "Logged Out")
{
m365 login
}
Write-Host "Connected to M365"
foreach ($row in $inputSites)
{
Write-Host "Getting configuration details from CSV"
$rootSiteUrl = $row.RootSiteUrl
$siteTitle = $row.SiteTitle
$siteUrl = $row.SiteUrl
$siteAlias = $row.Alias
$siteType = $row.Type
$completeSiteUrl = $rootSiteUrl + $siteUrl
$site = $null
try
{
$site = m365 spo site get --url $completeSiteUrl | ConvertFrom-Json
}
catch
{
Write-Host "Exception: $($_.Exception.Message)"
}
if ($null -eq $site)
{
if ($siteType -eq "TeamSite")
{
# Creating a SharePoint team site
m365 spo site add --type $siteType --title $siteTitle --alias $siteAlias --isPublic
Write-Host "Site collection $($completeSiteUrl) created successfully!" -foregroundcolor Green
}
else
{
# Creating a SharePoint communication site
m365 spo site add --type $siteType --title $siteTitle --url $completeSiteUrl
Write-Host "Site collection $($completeSiteUrl) created successfully!" -foregroundcolor Green
}
}
else
{
Write-Host "Site collection $($completeSiteUrl) exists already!" -foregroundcolor Yellow
}
}
# Disconnect M365 connection
m365 logout
Write-Host "Script execution completed successfully!" -foregroundcolor Green
Contributors
| Author(s) |
|---|
| Kshitiz Kalra |
| Ganesh Sanap |
Disclaimer
THESE SAMPLES ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
Tool documentation & references
- Want to learn more about PnP PowerShell and the cmdlets
Check out the PnP PowerShell site to get started and for the reference to the cmdlets.
- Want to learn more about CLI for Microsoft 365 and the commands
Check out the CLI for Microsoft 365 site to get started and for the reference to the commands.