Create multiple SharePoint sites using CSV file

In a few days ago, I have been asked me to create a couple of sites and I create a simple PowerShell script using PnP Framework. Creating these sites on the SharePoint administration centre can take a while since you can only create one at the same time. If you are using a script like this will allow you to do a bit more on that also allow you to run other settings that you want to enable/disable on the site. 

The first step is to make sure you have the latest version of the PnP framework PowerShell installed, or the older version of PnP PowerShell. Check this link to know how to install this new version:

Then our CSV file will have the following structure. 

SiteName 
Marketing 

If you need things like the custom scripts, for instance, you can add a new column and put and 1 or 0 if you want to enable/disable the setting. 

Then save this file under the same place as the PowerShell script with the name sites.csv.  Note that on this example, I’m creating Communication Sites if you want to switch, you just need to the PowerShell to uncomment the line below and comment the other. You can also make this a flag.

$location = Split-Path -Parent $MyInvocation.MyCommand.Definition
$AdminUrl = 'https://sharetricks-admin.sharepoint.com/'
$rootUrl = 'https://sharetricks.sharepoint.com/'
$CSVFileName = 'sites.csv'

$sites = Import-Csv "$location\$CSVFileName"
Connect-PnPOnline $AdminUrl -UseWebLogin
foreach ($site in $sites) {
  try {
    #New-PnPSite -Type TeamSite $site.SiteURL -Url ("{0}/sites/{1}" -f $rootUrl, $site.SiteURL.Replace(' ', '')) -Wait  
    New-PnPSite -Type CommunicationSite -Title $site.SiteURL -Url ("{0}/sites/{1}" -f $rootUrl, $site.SiteURL.Replace(' ', '')) -Wait    
  }
  catch [System.Net.WebException], [System.IO.IOException] {
    Write-host "Unable to apply template to $site.SiteURL" -ForeGroundColor Red
  }
}

When you run the script, the site will be created and note that if the SharePoint site already exists, it will fail and the message should be that the site already exists.  

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *