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 :

To create a site collection in SharePoint, use this PowerShell script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Parameters to create new site collection
$SiteName = "Demo"
$SiteURL= "https://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:

You can use PowerShell to bulk create SharePoint site collections from a CSV file. In this blog post, we will show you how to create multiple site collections in SharePoint from a CSV file using PowerShell. This can be useful if you need to quickly create multiple site collections for a new project or initiative.

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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

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

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

    I get the error message when execute the script

    Reply
    • 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
  • 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

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