Create Site Collections in Bulk from a CSV File using PowerShell in SharePoint 2016

PowerShell cmdlet New-SPSite is used to create new site collections in SharePoint. It takes URL and Owner mandatory parameters with other parameters. Here is an example:

Create SharePoint Site Collection Using PowerShell :

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Parameters to create new site collection
$SiteName = "Demo"
$SiteURL= "http://intranet.Crescent.com/sites/demo"
$SiteOwner = "Crescent\SPAdmin"
$SiteTemplate = "STS#0"
 
#create site collection with given parameters
New-SPSite -Url $URL -Name $SiteName -OwnerAlias $SiteOwner -Template $SiteTemplate

PowerShell to Create Multiple Site Collections in Bulk from a CSV File:

Let’s add some error handling and wrap New-SPSite into a reusable PowerShell function, to create multiple site collections from a CSV file.

  • Step 1: Create a CSV file in the below format. Populate the column values according to your requirements.
    create site collections from csv in sharepoint
  • Step 2: Use this PowerShell script to read from CSV and create site collections in bulk.
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Function to Create new Site Collection
Function Create-SPSite
{
  param
    (
        [string]$Name  = $(throw "Please Provide the Site Name!"),
        [string]$URL = $(throw "Please Provide the Site URL!"),
        [string]$Owner = $(throw "Please Provide the Site Owner!"),
        [string]$Template = $(throw "Please Provide the Site Template!")
    )
Try {
   #Set the Error Action
    $ErrorActionPreference = "Stop"
 
    #Check if the site collection exists already
    $SiteExists = Get-SPSite | where {$_.url -eq $URL}
    #Check if site exists in the recycle bin
    $SiteExistsInRecycleBin = Get-SPDeletedSite | where {$_.url -eq $URL}
  
    If($SiteExists -ne $null)
    {
        write-host "Site $($url) exists already!" -foregroundcolor red
    }
    elseIf($SiteExistsInRecycleBin -ne $null)
    {
        write-host "Site $($url) exists in the recycle bin!" -foregroundcolor red
    }
    else
    {
        #create site collection
        New-SPSite -Url $URL -Name $Name -OwnerAlias $Owner -Template $Template
        write-host "Site Collection $($url) Created Successfully!" -foregroundcolor Green
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
    }
Finally {
    #Reset the Error Action to Default
    $ErrorActionPreference = "Continue"
 } 
}
 
#Read from CSV and create site collection
Import-Csv "C:\SitesToCreate.csv" | Foreach-Object { 
   Create-SPSite -Name $_.SiteName -URL $_.SiteURL -Owner $_.SiteOwner -Template $_.SiteTemplate  }

This script uses the combination of Import-Csv and SharePoint cmdlet to create multiple site collections in SharePoint. There are other parameters such as Content Database, Language, Site description, etc you can incorporate with this.

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

3 thoughts on “Create Site Collections in Bulk from a CSV File using PowerShell in SharePoint 2016

  • May 8, 2020 at 7:43 AM

    Error: Cannot bind argument to parameter ‘Url’ because it is an empty string.

    I get the error message when execute the script

    Reply
    • May 8, 2020 at 8:18 AM

      That means, The URL parameter you passed to the function is null! Just make sure your CSV file has exactly the same column names and has valid URLs in it!

      Reply
  • January 1, 2020 at 3:53 PM

    Error: A parameter cannot be found that matches parameter name ‘Name’.

    I get the above error message on executing the script

    Reply

Leave a Reply