Sunday, June 5, 2016

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= ""
$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:
Lets 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
            [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
            #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.

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.

Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...