SharePoint content database tutorial

In this SharePoint tutorial, we will discuss about SharePoint content database. We will check:

  • How to Find SharePoint content database name for site collection using PowerShell
  • How to get SharePoint content database size using PowerShell
  • How to Move SharePoint databases to new SQL server
  • Get content database sizes for all web applications in a SharePoint farm using PowerShell
  • Get content database sizes for all web applications in a SharePoint farm using PowerShell
  • Attach or detach content databases in SharePoint 2013/2016
  • Detach content database using SharePoint Central Administration
  • Attach SharePoint content database using PowerShell
  • Detach SharePoint content databases using PowerShell
  • Prevent new sites to be added to content databases in SharePoint

How to Find SharePoint content database name for site collection using PowerShell

Let us check out, how to find content database name for site collection using PowerShell in SharePoint 2013/2016/2019. We can easily know which content database one SharePoint site collection is using in SharePoint using PowerShell as well as from SharePoint central administration.

First, we will see how to find the content database name for site collection from SharePoint 2013/2016 central administration.

Open SharePoint 2013/2016 central administration. Then click on Application Management.

Then under Site Collections, click on View all site collections.

In this page you can see the particular site collection and then in the right side you can see the content database name like below:

SharePoint content database
Find SharePoint content database name for site collection using PowerShell

Using PowerShell also we can be able to know which content database one site collection is using in SharePoint 2013/2016.

You can execute the below PowerShell code in Windows PowerShell ISE.

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

(Get-SPSite "http://mypc:29024/sites/HydTraining/").ContentDatabase.name
Find SharePoint content database name for site collection using PowerShell
Find SharePoint content database name for site collection using PowerShell

This is how to find content database name for site collection using PowerShell in SharePoint 2013/2016/2019.

Get SharePoint content database size using PowerShell

Let us see, how to retrieve SharePoint content database size using PowerShell. The same PowerShell script code, we can use to get content database size using PowerShell in SharePoint 2013/2016. We can also use the server object model code to get SharePoint 2013/2016 content database size.

Below is the PowerShell command to get the SharePoint content database size using PowerShell.

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue

Get-SPDatabase | Sort-Object disksizerequired -desc | Format-Table Name, @{Label =”Size in MB”; Expression = {$_.disksizerequired/1024/1024}}

If you want to download in CSV or .txt format, then you can write code like below:

Get SharePoint content database size using PowerShell
get SharePoint content database size using PowerShell
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
Get-SPDatabase | Sort-Object disksizerequired -desc | Format-Table Name, @{Label =”Size in MB”; Expression = {$_.disksizerequired/1024/1024}} >E:\Content_DBsize.csv
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
Get-SPDatabase | Sort-Object disksizerequired -desc | Format-Table Name, @{Label =”Size in MB”; Expression = {$_.disksizerequired/1024/1024}} >E:\Content_DBsize.txt

Once you run the command, this will display like below:

Get SharePoint Content database size using SharePoint server object model

Below code, you can use to get content database size using SharePoint server object model code.

string s = string.Empty;
using (SPSite startSite = new SPSite(“http://mypc:29024/sites/SPTraining/”))
{
SPFarm farm = startSite.WebApplication.Farm;
SPWebService service = farm.Services.GetValue<SPWebService>(“”);
foreach (SPWebApplication webApplication in service.WebApplications)
{
foreach (SPContentDatabase contentdatabase in webApplication.ContentDatabases)
{
s += “Name: ” + contentdatabase.Name + “Size: ” + contentdatabase.DiskSizeRequired/(1024*1024)+” MB”+ “\n”;
}
}
}
label11.Text = s.ToString();

This is how to get content database size using PowerShell in SharePoint 2016 and using SharePoint server object model.

Get content database sizes for all web applications in a SharePoint farm using PowerShell

Let us see, how to get content database size for all web applications in a sharepoint farm using PowerShell in SharePoint 2013/2016.

This script allows getting the size for all the content databases available in a SharePoint Farm. The script defines a function (Get-ContentDBSizes) that gets all the Web Applications existing in a SharePoint Farm. For each Web Application, the function also gets the information.

All you got to do is spin up PowerShell on any SharePoint server in the farm and run this:

#############################################################
# Script that gets content databases sizes for all the web applications in a SharePoint Farm
# Parameters: N/A
#############################################################

If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )
{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell }
$host.Runspace.ThreadOptions = "ReuseThread"
#Function that gets the size of all the content databases
function Get-ContentDBSizes
{
try
{
$spWebApps = Get-SPWebApplication -IncludeCentralAdministration
foreach($spWebApp in $spWebApps)
{
#$spWebApp.Name
$ContentDatabases = $spWebApp. WSS_Content_Name (Change here DB name)
foreach($ContentDatabase in $ContentDatabases)
{
$ContentDatabaseSize = [Math]::Round(($ContentDatabase.disksizerequired/1GB),2)
$ContentDatabaseInfo= $spWebApp.DisplayName + "," + $ContentDatabase.Name + "," + $ContentDatabaseSize + " GB"
$ContentDatabaseInfo
#Write-Host " * " $spWebApp.DisplayName "-" $ContentDatabase.Name ": " $ContentDatabaseSize " GB"
}
}
}
catch [System.Exception]
{
write-host -f red $_.Exception.ToString()
}
}

Start-SPAssignment –Global
Get-ContentDBSizes > ContentDBs.csv
Stop-SPAssignment –Global
Remove-PsSnapin Microsoft.SharePoint.PowerShell

Once all modification done, Click on Run button on the PowerShell ISE. Like below:

get content database for site collection powershell
get content database for site collection powershell

After running successfully we will get result like below:

get content database size sharepoint 2013 powershell
get content database size sharepoint 2013 powershell

Open that .CSV file and verified all content DB size, below is the result sheet:

get content database size sharepoint 2016 powershell
get content database size sharepoint 2016 powershell

Here, we learned how to get content database sizes for all web applications in a SharePoint farm using PowerShell.

Attach or detach content databases in SharePoint 2013/2016

Let us see how to attach or detach content databases in SharePoint 2013/2016 using PowerShell or by using SharePoint central administration.

We can attach or detach a content database using SharePoint Central Administration or by using PowerShell commands.

Follow the below steps to attach a content database using SharePoint central administration.

First Open SharePoint Central Administration, then click Application Management.

On the Application Management page, in the Databases section, click Manage content databases.

On the Manage Content Databases page, click Add a content database.

This will open the Add Content Database page. Here in this page:

  • We need to select a particular web application.
  • We need to specify the database server.
  • Database Name, And the authentication mode and if you are using Sql Server authentication mode then we need to give the account name and password.

Then Click on OK.

Detach content database using SharePoint Central Administration

Follow the below steps to detach content database using SharePoint central administration.

First Open SharePoint Central Administration, then click Application Management.

On the Application Management page, in the Databases section, click Manage content databases.

Then select the web application for which you want to detach a content database.

Click the content database that you want to detach.

On the Manage Content Database Settings page, select the Remove content database checkbox.

Then click on OK. Then this will ask for Confirmation and then click on OK again.

Attach SharePoint content database using PowerShell

We can also easily attach a SharePoint content database using PowerShell.

First Open PowerShell and run the below command:

Mount-SPContentDatabase "<ContentDb>" -DatabaseServer "<DbServer>" -WebApplication http://SiteName

Detach SharePoint content databases using PowerShell

We can also detach a SharePoint content database using PowerShell in SharePoint.

First Open PowerShell and run the below command:

Dismount-SPContentDatabase "<ContentdBName>"

Prevent new sites to be added to content databases in SharePoint

Now, let us see how to prevent new sites to be added to content databases that grow more in database size in SharePoint 2013/2016.

Suppose the content databases have grown larger than 100 gigabytes (GB). Large content databases can be difficult to back up and restore. And sometimes this causes the application to stop responding when you perform operations that affect entire databases.

Here we can edit the rule definition to prevent new sites from being added to these databases.

We can do this from SharePoint Central Administration.

Open SharePoint Central Administration, On the Monitoring page, in the Health Analyzer section, click Review rule definitions.

Then On the Health Analyzer Rule Definitions page, in the Availability category, click the name of the rule.

In the Health Analyzer Rule Definitions dialog box, click Edit Item, and then select the Repair Automatically checkbox.

Click Save. You can no longer add new sites to databases that exceed 100 GB in SharePoint.

Here, we learned how to attach or detach content databases in SharePoint 2013/2016.

How to Move SharePoint databases to new SQL server

Let us see, the need to move SharePoint Databases to new SQL Server and how to move the SharePoint database to the new SQL Server.

SQL Server is a relational database service on the leading market of the SQL Server engine. It provides various performances, business continuity, scalability with no downtime, data protection, etc.

It is mainly used on the organization platforms for data integration, data analysis, and processing components. It is very stable, popular, affordable, as well as a fast database engine.

Why do users need to move SharePoint database to SQL Server?

SharePoint is a web-based application, which enables an organization to work more professionally by allowing the users to share data, information, and documents. There are various SharePoint products for an organization, which offers a unique set of advantages to users.

However, there is some circumstance that is faced by users while using SQL Server such as – SharePoint has some file size limit because of which it creates a problem in synchronization of the file. It even blocks some file types to be uploaded on SharePoint. It does not have any profile pages for any external content type. It does not work in offline mode; it needs proper internet connectivity to work. Even, it does not have any search integration.

To overcome such shortcomings, many users are moving their data from SharePoint databases to the new SQL Server.

Move SharePoint databases to new sql server

The user can easily move the SharePoint database to a new SQL Server by following the steps given below:

Run CMD in administration mode and type the following command:

c:\windows\system32>cliconfg.exe

Now a client-server-client network utility window will appear, enable the TCP/IP on it. Select Alias >> Add > type the same name for server >> OK

Then Open SharePoint management shell and type a command.

psc:\users\mradwan\get-database

Now the entire database for SharePoint will be listed.

Copy all the GUID and paste it into a notepad file.

Now type a command on SharePoint management shell

psc:\users\mradwan>$db-get-spdatabase

Write the following command and then type an update command

psc:\users\mradwan\$db.ChangeDatabaseInstance<“TFS”

Follow the above two steps for all GUID’s

Now, Start all the SharePoint services that were stopped earlier and Open SQL administration and enter credentials.

Now, you can easily view your entire SharePoint database in SQL Server. The above blog discusses a procedure for migrating SharePoint database to SQL Server with the combined usage of CMD and PowerShell. Users need to use cmdlet for moving the data from one database to a new server however, a set of requirements needs to be fulfilled beforehand to make the procedure feasible.

Here, we learned the need to move SharePoint Databases to a new SQL Server and how to move the SharePoint database to a new SQL Server.

You may like the following SharePoint tutorials:

In this tutorial, we discussed about SharePoint content database tutorial. And,

  • How to Find SharePoint content database name for site collection using PowerShell
  • Get SharePoint content database size using PowerShell
  • Get SharePoint Content database size using the SharePoint server object model
  • How to Move SharePoint databases to new SQL server
  • Why do users need to move the SharePoint database to SQL Server?
  • Move SharePoint databases to new sql server
>