GitHub Icon Image
GitHub

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
  • PnP PowerShell
  • CLI for Microsoft 365

$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 ?


$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

Check out the CLI for Microsoft 365 to learn more at: https://aka.ms/cli-m365

Important changes coming to the way you login into CLI for Microsoft 365 (effective 9th September 2024) see Changes in PnP Management Shell registration in Microsoft 365

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.

Back to top Script Samples
Generated by DocFX with Material UI