Administering SharePoint 2013 with PowerShell: Managing Content Databases

Content databases contain all of the user data for one or more site collections. A site collection, which is a logical container for one or more sites, can only store its content in one content database, but a single content database can hold multiple site collections inside it. Similarly, content databases can belong only to one web application, but one web application can contain multiple content databases. Simple, right? Because content databases are so important to SharePoint, you should really be comfortable working with and administering them. This article is going to show you how to use PowerShell to adminster SharePoint 2013; specifically, how to administer and manage content databases.

Administering SharePoint 2013 Using PowerShell: Prerequisites

You need farm administrator access to a SharePoint 2013 farm to complete the exercises in this article.

  • Open a remote desktop session to one of your SharePoint servers in your farm.
  • Open PowerShell or the SharePoint Management Shell.
  • If you use PowerShell ISE or if you do not have the SharePoint commands available in the shell you’re using, load the SharePoint commands with this PowerShell command: Add-PSSnapin Microsoft.SharePoint.PowerShell

Find the SharePoint 2013 Content Databases with PowerShell

When working with your content databases, you’ll use the Get-SPContentDatabase cmdlet to find and list them. This command will be the foundation of working with your content databases, so let’s get familiar with it.

List all content databases in SharePoint 2013 with PowerShell

Use the Get-SPContentDatabase with no parameters to get the list of all content databases in your SharePoint 2013 farm.

Get-SPContentDatabase

List all content databases in a specific web application

Use the –WebApplicationto specify all of the content databases used by a specific web app.

Get-SPContentDatabase –WebApplication “http://mysharepointapp.local”

Show a single content database using PowerShell

Use the –Identityparameter of Get-SPContentDatabase to return a single content database. The Identity can be either the friendly name of the content database or the actual ID of the database, which is a long GUID that you’ll only get at by scripting or copy/pasting it.

Get-SPContentDatabase –Identity “WSS_Content”
Get-SPContentDatabase –Identity “GUID-OF-DATABASE-123456-1234”

You can also omit the -Identity parameter. If you add in a name or a GUID, Get-SPContentDatabase knows what you’re asking for and returns the content database.

Administering SharePoint 2013 with PowerShell: Identity cmdlet

Find which Site Collections are using a content database

Isolate the content database by using the Identity parameter of Get-SPContentDatabase, then send the SPContentDatabase object through the pipeline to a Get-SPSitecmdlet.

Get-SPContentDatabase “WSS_Content” | Get-SPSite

Find what content database a SharePoint 2013 site collection is using

Remember that in SharePoint 2013 a site collection can only store its content in a single content database. To find out which content database a particular site collection is using, use the -Site parameter on the Get-SPContentDatabase cmdlet.

Get-SPContentDatabase –Site “http://your.sharepoint.local/sitecollection”

Create New Content Databases in SharePoint 2013 with PowerShell

Creating new content databases is no problem, and the beautiful thing is that you can create as many content databases as you want just as easily as you can creating only one with PowerShell and the SharePoint Management Shell.

We use the New-SPContentDatabase cmdlet to create the databases. It requires only a few simple specifications for your new database. You must provide it a name for the database, and you must assign it a web application. Optionally, you can specify the maximum number of sites that can use the database with the -MaxSiteCount parameter or specify an alternate SQL server instance to create the database on with the -DatabaseServer parameter.

Create a new content database for a web application

Creating a content database can be done with only two parameters, -Name and -WebApplication.

New-SPContentDatabase –Name “HRContentDB” –WebApplication “http://StaffPortal”

If you have multiple departments with their own site collections, and you want to keep of their content in a separate database, you can easily create all of their content databases at once.

“HR”,”Legal”,”Sales”,”Marketing”,”Accounting” | Foreach-Object {
New-SPContentDatabase “$($_)_ContentDB” “http://StaffPortal”
}

While you cannot pipe items directly into the New-SPContentDatabase, it expects that the first thing you input is the name off the content database, and the second thing you enter is the web application. If you enter those two parameter values in that order, you don’t have to specify the parameter names.

New-SPContentDatabase “HRContentDB” “http://StaffPortal

It works just the same as specifying “-Name” and “-WebApplication.”

Administering SharePoint 2013 with PowerShell: BatchCreation

Note: The “$($_)_” is used to put the “HR,””Legal,” etc. in line with the rest of the database name. Specifying just “$__Content” would leave the string messed up since it can’t properly resolve the variable name. Use a $($_) to resolve variable that is inside the parenthesis as a variable. Once it’s resolved, it places it in the string. Try it for yourself by entering “HR”,”Legal” | % {“$($_)_Content”} into PowerShell.

Administering Content Databases with PowerShell

You can make several changes to content databases with the Set-SPContentDatabase. Just like when you piped the results of Get-SPContentDatabase into Get-SPSite to find sites that are attached to it, you can pipe the results of Get-SPContentDatabase into Set-SPContentDatabase to perform administration on those databases.

The Set-SPContentDatabase will find content databases by name or GUID without needing the Get-SPContentDatabase to process first. But if you want to work with the content database of a particular site then you’ll need to use the “-Site” parameter from Get-SPContentDatabase, because that parameter doesn’t work in Set-SPContentDatabase.

Examples of what will work:

Set-SPContentDatabase “HR_ContentDB”

Get-SPContentDatabase “http://hr.sharepoint.local” | Set-SPContentDatabase

Move a site collection to a different content database

This property is owned by the site, not the content database, so perform this operation using the SPSite cmdlets.

Get-SPSite “http://legal.sharepoint.local” | Move-SPSite –DestinationDatabase “Legal_ContentDB”

Set the maximum number of sites a content database can have

Use the –MaxSiteCount parameter to specify a positive integer.

Set-SPContentDatabase “HR_ContentDB” –MaxSites 1

You can also specify a warning number to get an alert when the maximum number of sites is approaching.

Set-SPContentDatabase “HR_ContentDB” –MaxSites 10 –WarningSiteCount 7

Moving a Content Database to a Different Web Application

Moving a content database to a different web application is a two-step process. First, you have to dismount the database from its current web application. Second, you can attach it to a different web application.

Get-SPContentDatabase “Marketing_ContentDB” | Dismount-SPContentDatabase –Confirm:$False
Mount-SPContentDatabase –Name “Marketing_ContentDB” –WebApplication “http://marketing.sharepoint.local” –Confirm:$false

Removing Content Databases in SharePoint 2013 with PowerShell

Use the Remove-SPContentDatabase cmdlet to delete them completely from the SQL server, or use Dismount-SPContentDatabase to remove an unwanted content databases from your farm but still leave it on the SQL Server without deleting it.

Remove a content database from the farm but not delete it on the SQL server

The Dismount-SPContentDatabase cmdlet doesn’t delete the content database. This makes it easy to migrate the database to a different web application or SharePoint farm. Note that this is the same command we used as the first step in moving the content database to a different web application.

Dismount-SPContentDatabase –Identity “HR_ContentDB”

Remove and delete a content database from the farm

The Remove-SPContentDatabase removes the content database and deletes it completely.

Remove-SPContentDatabase –Identity “HR_ContentDB”

Wrap-Up

Working with content databases doesn’t have to be tedious or frustrating. Your content databases are so important to your SharePoint environment, and being able to work with them and manage them in exactly the way you want is such a great help.

Did this article help you?  Let me know in the comments below, or help a friend by sharing it with them. If you have questions about this, reach out to me on Facebook, Twitter, or my personal blog.