Install the PnP PowerShell module if not already installed
Install-Module -Name PnP.PowerShell
Connect to SharePoint Online with interactive authentication
$AdminURL = “https://yourtenant-admin.sharepoint.com”
Connect-PnPOnline -Url $AdminURL -Interactive
Import site information from the CSV file
$SiteCollections = Import-Csv -Path “C:\Path\To\Your\CSVFile.csv”
$HubSiteId = “YOUR_HUB_SITE_ID” # Replace with your Hub site ID
Define document libraries based on templates
$DocumentLibraryNames = @{
“STS#0” = @(“TeamDocuments”, “SharedDocs”, “ProjectFiles”)
“STS#1” = @(“Announcements”, “MediaLibrary”, “MarketingDocs”)
“STS#2” = @(“ProjectDocuments”, “ResearchDocuments”, “Reports”)
“STS#3” = @(“PolicyDocuments”, “Templates”, “Archive”)
}
foreach ($site in $SiteCollections) {
$Title = $site.Title
$URL = $site.Url
$Owner = $site.Owner
$Template = $site.Template
# Create the site collection based on the specified template try { New-PnPTenantSite -Url $URL -Title $Title -Owner $Owner -Template $Template -NoWait Write-Host "Created site collection: $Title at $URL" -ForegroundColor Green } catch { Write-Host "Error creating site collection. It may already exist: $_" -ForegroundColor Red continue } # Delay to ensure the site is fully provisioned Start-Sleep -Seconds 30 # Adjust time as necessary based on your tenant's provisioning speed # Create Document Libraries based on the template if ($DocumentLibraryNames.ContainsKey($Template)) { foreach ($libraryName in $DocumentLibraryNames[$Template]) { # Create the Document Library New-PnPList -Title $libraryName -Template DocumentLibrary -Web $URL Write-Host "Document library created: $libraryName" -ForegroundColor Green # Add the newly created document library to the left navigation $LibraryUrl = "$URL/$libraryName" Add-PnPQuickLaunchItem -Title $libraryName -Url $LibraryUrl -Parent "RootNode" Write-Host "Added $libraryName to left navigation" -ForegroundColor Green } } else { Write-Host "No document libraries defined for the template: $Template" -ForegroundColor Yellow } # Associate the newly created site to the Hub site try { Set-PnPHubSite -Site $URL -HubSiteId $HubSiteId Write-Host "Added $Title to Hub site with ID: $HubSiteId" -ForegroundColor Green } catch { Write-Host "Failed to add $Title to Hub site. Error: $_" -ForegroundColor Red }
}