Skip to main content

PowerShell To Manage Content Databases

Content Database is the most important database used in SharePoint. SharePoint Administrators and DBA may find it useful to use PowerShell to manage content databases.

I think PowerShell is the best tool for managing content databases, hence I will describe some cmdlets to use in PowerShell to manage content databases.



Before we begin, an important thing to note is that to manage content databases through PowerShell, user needs to have proper permissions to do so. He needs to be a member of the SharePoint_Shell_Access database role in the respective databases. SharePoint Farm Administrator can add the user to this role using Add-SPShellAdmin cmdlet.

To add a user to SharePoint_Shell_Access database role in farm configuration database and specific content database, use following command:
Add-SPShellAdmin -UserName DOMAIN\USERNAME -Database DATABASENAME

To add a user to SharePoint_Shell_Access database role in farm configuration database and all content databases, use following command:
Get-SPContentDatabase | Add-SPShellAdmin -UserName DOMAIN\USERNAME

Now, let us start getting familiar with the cmdlets.

Get information about content database(s):

The command given without parameters retrieves all the information of one or more content databases available in the farm. It will display information like Id: Content database ID, Name: Name of content database, WebApplication: associated web application, Server: Database server name, CurrentSiteCount: No. of site collections in the content database.
Get-SPContentDatabase

To, get the information of the particular content database, pass the parameter -identity followed by the content database name.
Get-SPContentDatabase -Identity <contentdbname>

Create a new content database in a web application:

To create a new content database, user need to pass name of the content database, database  server name, name of the webapplication and maximum no. of sitecolelctions to be allowed to store in the content database. MaxSiteCount value can be changed by Set-SPContentDatabase cmdlet which is described later in this post.
New-SPContentDatabase -Name <contentdbname> -DatabaseServer <dbservername> -WebApplication <webappname> -MaxSiteCount 1 -WarningSiteCount 0

Detach a content database from a web application:

If a user wants to move the content database to some other web application or to some another database server then, Dismount-SPContentDatabase cmdlet comes to the rescue.
Dismount-SPContentDatabase -Identity <contentdbname>

Attach a content database to a web application:

As described in previous command, when user has dismounted the content database to move it to other webapp, then using the Mount-SPContentDatabase cmdlet user can attach the content database to new webapplication for which it is moved, or attach the database to another database server.
Mount-SPContentDatabase -Name <contentdbname> -DatabaseServer <dbservername> -WebApplication <webappname>

Also, the Mount-SPContentDatabase cmdlet can be used for the upgrade of database say to a newer SharePoint version.

Remove a content database:

Remove-SPConteDatabase will detach the content database form web application and drop the database from SQL server instance.
Remove-SPContentDatabase -Identity <contentdbname>

Set properties of a content database:

Set-SPContentDatabase can be used to set properties like MaxSiteCount (maximum no. of sitecollections to allow to be stored in the database), WarningSIteCount (a warning notification to be set when the limit is reached) and Status (disabled or online).
Set-SPContentDatabase -Identity <contentdbname> -Status OnLine

Test a content database:

Test-SPContentDatabse will test the content database against web application to confirm that the customization done are available in web application. If not then it will display the errors or warning for the user to know.
Test-SPContentDatabase -Identity <contentdbname>

Thus, these cmdlets can be used for the basic operations to manage content databases.

Enjoy using powershell cmdlets... 😊

Comments