Create Site Collection with Content Database

A PowerShell related post today.

If you need to create a new site collection and ensure it uses a specific content database, this little script will get that done for you. No messing about in Central Administration disabling content databases and all that good stuff, just a simple script that gets the job done, hassle free.

$hostIP = $ENV:ComputerName
$hostURL = "http://$hostIP/sites/Site Collection Name"
$user = [Security.Principal.WindowsIdentity]::GetCurrent().Name
$dbServer = "$hostIP"
$contentDbName = "Your_Content_Db_Name"
$webAppName = "SharePoint - 80"
$lcid = 1033
$templateName = "STS#0"
$siteName = "Site Collection Name"
function Add-Site {
	Write-Host ("Creating new ComplianceSP site '", $siteName, "' at ", $hostURL -join "") -NoNewline
	$newSite = New-SPSite $hostURL -OwnerAlias $user -name $siteName -Template $templateName -Language $lcid -ContentDatabase $contentDbName
	$newSite.RootWeb.CreateDefaultAssociatedGroups($newSite.Owner, $null, $null)
	Write-Host " - Done." -ForegroundColor Green
function Add-ContentDatabase {
	$contentDatabase = Get-SPContentDatabase -WebApplication $webAppName | Where { $_.Name -eq $contentDbName }
	if ($contentDatabase -eq $null) {
		Write-Host ("Creating new content database '", $contentDbName, "' in '", $dbServer, "' for web application '", $webAppName, "'" -join "") -NoNewline
		New-SPContentDatabase -Name $contentDbName -DatabaseServer $dbServer -WebApplication $webAppName -MaxSiteCount 1 -WarningSiteCount 0 -Confirm:$false
		Write-Host " - Done." -ForegroundColor Green
	else {
		Write-Host ("Content database '", $contentDbName, "' in '", $dbServer, "' associated with web application '", $webAppName, "' already exists." -join "") -NoNewline
try {
catch {
	Write-Host ""
	Write-Host "Error : " $Error[0] -ForegroundColor Red
	Exit 1

Lets walk through the code.

First off we declare a number of variables that are used to create the site collection and associate it with a specific content database.

$hostURL stores the fully qualified URL of the site collection you want to create.
$dbServer references the database server, as you probably guessed 🙂
$contentDbName should contain the name of the content database you want to associate with the site collection
$webAppName is the name of the web application the site collection will belong to.
$lcid is the locale id to be used when creating the site collection.
$templateName is the name of the SharePoint template to use for the root of the new site collection.
$siteName contains the name of the root site.

The first section of code that is called is to Add-ContentDatabase. What this method does is attempt to retrieve the content database specified by $contentDbName within the configured web application. If the content database exists the method exits without doing anything else. If it does not exist, it then calls New-SPContentDatabase and creates the content database with a maximum site count of 1, this will allow it to only be used by the site collection that’s created next.

The final piece of the script is a call to the Add-Site method.

Here, we call New-SPSite and importantly set the content database parameter to $contentDbName. Once this has completed we ensure the new site’s owner is a member of the site owners group.

Nothing too complicated by handy to know.

This entry was posted in SharePoint and tagged , . Bookmark the permalink.

Leave a Reply

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

Solve the maths problem shown below before posting: *