PowerShell: Create a SharePoint Site Collection in an Independent Content Database

Introduction

In SharePoint 2016, Although the content database can hold up to 10000 site collections (5000 recommended) and the site collection can be as large as the content database size limit for the general usage scenario but it's strongly recommended

  • limiting the size of content databases to 200 GB.
  • limiting the size of site collections to 100 GB.

For more details, check Software boundaries and limits for SharePoint Server 2016.

In this article, we will introduce a simple PowerShell Script to create a Site Collection in a separate Content Database.

Note: The supported number of Content Databses per farm is 500 Content Database, With 200GB per content database, and 500 content databases per farm, SharePoint Server 2016 supports 100TB of data per farm.  For more details check SharePoint 2016: Content Database limits compared to previous versions.

Script in Details

"Create a Site Collection in a separate Content Database" script includes the below sections

  • Variables Definition.
  • Get the SQL Server Instance.
  • Create a Content Database.
  • Lock the Content Database.
  • Create a Managed Path:
    • Wildcard inclusion.
    • Explicit inclusion.
  • Create a Site Collection in the newly created Content Database.

Download the full script from PowerShell: Create a SharePoint Site Collection in an Independent Content Database in TechNet Gallery.

Variables Definition

The below table define the Script Variables:

 Variable
 Definition
$WebAppUrl
The Web Application URL
$ManagedPath The default managed path is "sites", meanwhile you can create a new Managed Path as you prefer
$wildcard Type "Y" for Wildcard inclusion, "N" for Explicit inclusion
$SiteCollectionName The Site Collection Name
$SiteCollectionURL The Site Collection URL
$SiteCollectionTitle The Site Collection Title
$Template The site template code, to get all Templates name run Get-SPWebTemplate
$OwnerAlias The primary Site Collection Administrator
$Language The default site language, type 1025 for Arabic in case you have installed the Arabic Language Pack
$ContentDB The content database name must be unique so it will depend on the site collection name
#######################################################
#Variabes Defination
#######################################################
#The Web Application URL
$WebAppUrl =  "http://WebAppURL" 
#The default managed path is  "sites", meanwhile you can create a new Managed Path as you prefer
$ManagedPath ="sites" 
#Type "Y" for  Wildcard inclusion, "N" for Explicit inclusion
$wildcard = "Y" 
#The Site Collection Name
$SiteCollectionName = "mqassas" 
#The Site Collection URL
$SiteCollectionURL = $WebAppUrl +"/"+ $ManagedPath + "/"+ $SiteCollectionName
#The Site Collection Title
$SiteCollectionTitle = "MQassas New Site Collection"
#The site template code (Default is  Team Site), to  get all Templates name run Get-SPWebTemplate 
$Template = "STS#0"
#The primary Site Collection Administrator
$OwnerAlias = "domain\user name"
#The default site language, type  1025 for  Arabic in  case you have installed the Arabic Language Pack
$Language ="1033" 
#The content database name must be unique so it will depend on  the site collection name
$ContentDB = "WSS_Content_" + $SiteCollectionName
#################################################################################

Get the SharePoint SQL Server Instance 

First, we get the SQL Server Instance that hosts the SharePoint Databases.

#Get the SQL Server Instance
$DBServer = ((Get-SPDatabase)[0]).ServerName

Create a Content Database

Second, based on the Database server, we create a new Content Database that will hold the new Site Collection.

#Create A content Databse
 Write-Host "Create A new Content Database"  -ForegroundColor Green
 $result = New-SPContentDatabase $ContentDB -DatabaseServer $DBServer -WebApplication $WebAppUrl
 if($result -ne $null) 
        {
           Write-Host "The Content Database "  $ContentDB " has been created successfully" -ForegroundColor Cyan
        }
 else
        {
           Write-Host $_.Exception.Message -ForegroundColor Red
           return
        }

Lock down the newly created Content Database

In this step, we lock down the newly created content database to hold only one site collection and prevent add any new site collection.

#Lockdown the Content Database
 Write-Host "Lock Down the Content Database"  -ForegroundColor Green
 Set-SPContentDatabase -Identity $ContentDB -MaxSiteCount 1 -WarningSiteCount 0

Create Managed Path
There are two types of managed paths that you can create: (For more details check ManagedPath in SharePoint)

  • A wildcard inclusion allows you to append multiple site collections to the path that you specify.
  • An explicit inclusion allows you to create a single site collection with the specified address.

In this step, based on your defined setting in the variables definition, the script will create the new managed path of not exist. 

# Create  Managed path
$MPath = Get-SPManagedPath -WebApplication $WebAppUrl -Identity $ManagedPath -ErrorAction SilentlyContinue
           if ($MPath -ne $null)
             {
               Write-Host "Managed path $ManagedPath already exists."
             }
           else
             {
               Write-Host "Creating managed path $ManagedPath ..."
               if($wildcard -eq "Y")
               {
                 New-SPManagedPath –RelativeURL $ManagedPath -WebApplication $WebAppUrl 
               }
               else
               {
                 New-SPManagedPath –RelativeURL $ManagedPath -WebApplication $WebAppUrl -Explicit
               }
                Write-Host "Managed path $ManagedPath created sucessfully" -foregroundcolor Green
             }

Create a Site Collection

Based on managed path setting and the newly created Content database, you will be ready now to create a new Site Collection in an Independent Content Database.

#create Site Collection
Write-Host "Create a new Site Collection"  $SiteCollectionName " in"   $ContentDB -ForegroundColor Green
           if($wildcard -eq "Y")
             {
               $SiteCollectionURL = $WebAppUrl +"/"+ $ManagedPath + "/"+ $SiteCollectionName
             }
           else
             {
               $SiteCollectionURL = $WebAppUrl +"/"+ $ManagedPath 
             }
         
            $result = New-SPSite $SiteCollectionURL -Name $SiteCollectionTitle -OwnerAlias $OwnerAlias -Language $Language -Template $Template
            if($result -ne $null) 
               {
                 Write-Host "The Site Collection "  $SiteCollectionName " has been created successfully" -ForegroundColor Cyan
                 #browse New Site colection 
                 START $SiteCollectionURL
               }
            else
               {
                Write-Host "Error: The Site Collection is not created" -ForegroundColor Red
                return
               }

Once the site collection is created successfully, it will be browsed directly as shown below.

https://gallery.technet.microsoft.com/site/view/file/183011/1/PowerShell%20Create%20a%20Site%20Collection%20in%20an%20Independent%20Content%20Database%20Browse%20.png

Download the full script from PowerShell: Create a SharePoint Site Collection in an Independent Content Database in TechNet Gallery.

Output

https://gallery.technet.microsoft.com/site/view/file/182911/1/PowerShell%20Create%20a%20Site%20Collection%20in%20an%20Independent%20Content%20Database.png

Applies To

  • SharePoint 2016.
  • SharePoint 2013.
  • SharePoint 2010.

Download

Download the full script from PowerShell: Create a SharePoint Site Collection in an Independent Content Database in TechNet Gallery.

Conclusion

In this article, we have learned How to 

  • Get the SharePoint SQL Server Instance using PowerShell.
  • Create a Content Database in SharePoint using PowerShell.
  • Lockdown a Content Database in SharePoint using PowerShell.
  • Create a Managed Path in SharePoint using PowerShell.
  • Create a SharePoint Site Collection in a separate Content Database.

See Also

Back To Top