Creation of SharePoint Online sites 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://<tenant_name>.sharepoint.com | My Communication Site | /sites/mycommsite | CommunicationSite | |
https://<tenant_name>.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
Check out the PnP PowerShell to learn more at: https://aka.ms/pnp/powershell
The way you login into PnP PowerShell has changed please read PnP Management Shell EntraID app is deleted : what should I do ?
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.