Get SharePoint content database size using PowerShell and server object model

This SharePoint tutorial, we will discuss how to retrieve SharePoint content database size using PowerShell and using the SharePoint server object model. 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.

If you are new to SharePoint 2016, you can check installation steps of SharePoint 2016 and SharePoint 2016 new and improved features and SharePoint 2016 List View Auto Indexing Automatic Index Management.

PowerShell script to retrieve 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
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();

Hope this will be helpful to get content database size using PowerShell in SharePoint 2016 and using SharePoint server object model.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>