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
  }
}
Create multiple SharePoint sites using CSV file

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.  

This blog is part of SharePoint Week. For more great content, click here

About the Author:

My name is David Ramalho and I’m currently part of  Storm Technology Ltd family where I’m part of the Modern Workplace team.

In the past, I worked at BindTuning as a Support engineer and then as SharePoint development. The reason for creating this blog is to share tricks that can help on a daily basis for managing your SharePoint in different areas.

All the tricks, comments are welcome and feel free to contact me in case those tricks help, could be improved, say hello, etc.

Reference:

Ramalho, R. (2021). Create multiple SharePoint sites using CSV file. Available at: https://sharepoint-tricks.com/create-multiple-sharepoint-sites-using-csv-file/ [Accessed: 14th September 2021].

Share this on...

Rate this Post:

Share:

Topics:

SharePoint

Tags: