Create Multiple Site Collections from a CSV using PnP script

%3CLINGO-SUB%20id%3D%22lingo-sub-2140449%22%20slang%3D%22en-US%22%3ECreate%20Multiple%20Site%20Collections%20from%20a%20CSV%20using%20PnP%20script%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2140449%22%20slang%3D%22en-US%22%3E%3CP%3EHello%20All%2C%3C%2FP%3E%3CP%3ECreating%20bulk%20site%20collections%20very%20normal%20use%20case%2C%20Please%20find%20the%20below%20PS%20Script%20to%20create%20multiple%20site%20collections%20from%20your%20CSV%20file.%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%23Config%20Parameters%3CBR%20%2F%3E%24TenantUrl%20%3D%20Read-Host%20%22Enter%20SharePoint%20tenant%20admin%20URL%22%3CBR%20%2F%3E%24CSVPath%20%3D%20%22C%3A%5CTemp%5CSiteCollections.csv%22%3CBR%20%2F%3E%3CBR%20%2F%3E%23Connect%20to%20Tenant%3CBR%20%2F%3EConnect-PnPOnline%20-url%20%24TenantUrl%20-Credentials%20(Get-Credential)%20%23or%20-UseWebLogin%20for%20MFA%3CBR%20%2F%3E%3CBR%20%2F%3ETry%20%7B%3CBR%20%2F%3E%23Get%20Site%20Collections%20to%20create%20from%20a%20CSV%20file%3CBR%20%2F%3E%24SiteCollections%20%3D%20Import-Csv%20-Path%20%24CSVPath%3CBR%20%2F%3E%3CBR%20%2F%3E%23Loop%20through%20csv%20and%20create%20site%20collections%20from%20each%20row%3CBR%20%2F%3EForEach%20(%24Site%20in%20%24SiteCollections)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%23Get%20Parameters%20from%20CSV%3CBR%20%2F%3E%3CBR%20%2F%3E%24Title%20%3D%20%24Site.Title%3CBR%20%2F%3E%24Url%20%3D%20%24Site.Url%3CBR%20%2F%3E%24Owner%20%3D%24Site.Owner%3CBR%20%2F%3E%24Template%20%3D%20%24Site.Template%3CBR%20%2F%3E%24StorageQuota%20%3D%20%24Site.StorageQuota%3CBR%20%2F%3E%24TimeZone%20%3D%20%24Site.TimeZone%3CBR%20%2F%3E%3CBR%20%2F%3E%23Check%20if%20site%20exists%20already%3CBR%20%2F%3E%24SiteExists%20%3D%20Get-PnPTenantSite%20%7C%20Where%20%7B%24_.Url%20-eq%20%24URL%7D%3CBR%20%2F%3EIf%20(%24SiteExists%20-eq%20%24null)%3CBR%20%2F%3E%7B%3CBR%20%2F%3E%23Create%20site%20collection%3CBR%20%2F%3EWrite-host%20%22Creating%20Site%20Collection%3A%22%24Site.URL%20-f%20Yellow%3CBR%20%2F%3ENew-PnPTenantSite%20-Url%20%24URL%20-Title%20%24Title%20-Owner%20%24Owner%20-Template%20%24Template%20-TimeZone%20%24TimeZone%20-RemoveDeletedSite%20-ErrorAction%20Stop%3CBR%20%2F%3EWrite-host%20%22%60t%20Done!%22%20-f%20Green%3CBR%20%2F%3E%7D%3CBR%20%2F%3EElse%3CBR%20%2F%3E%7B%3CBR%20%2F%3Ewrite-host%20%22Site%20%24(%24URL)%20exists%20already!%22%20-foregroundcolor%20Yellow%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3ECatch%20%7B%3CBR%20%2F%3Ewrite-host%20-f%20Red%20%22%60tError%3A%22%20%24_.Exception.Message%3CBR%20%2F%3E%7D%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2140449%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Contributor

Hello All,

Creating bulk site collections very normal use case, Please find the below PS Script to create multiple site collections from your CSV file.


#Config Parameters
$TenantUrl = Read-Host "Enter SharePoint tenant admin URL"
$CSVPath = "C:\Temp\SiteCollections.csv"

#Connect to Tenant
Connect-PnPOnline -url $TenantUrl -Credentials (Get-Credential) #or -UseWebLogin for MFA

Try {
#Get Site Collections to create from a CSV file
$SiteCollections = Import-Csv -Path $CSVPath

#Loop through csv and create site collections from each row
ForEach ($Site in $SiteCollections)
{
#Get Parameters from CSV

$Title = $Site.Title
$Url = $Site.Url
$Owner =$Site.Owner
$Template = $Site.Template
$StorageQuota = $Site.StorageQuota
$TimeZone = $Site.TimeZone

#Check if site exists already
$SiteExists = Get-PnPTenantSite | Where {$_.Url -eq $URL}
If ($SiteExists -eq $null)
{
#Create site collection
Write-host "Creating Site Collection:"$Site.URL -f Yellow
New-PnPTenantSite -Url $URL -Title $Title -Owner $Owner -Template $Template -TimeZone $TimeZone -RemoveDeletedSite -ErrorAction Stop
Write-host "`t Done!" -f Green
}
Else
{
write-host "Site $($URL) exists already!" -foregroundcolor Yellow
}
}
}
Catch {
write-host -f Red "`tError:" $_.Exception.Message
}

0 Replies