Provision

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
powershell

$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

Contributors

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

Keep exploring