SharePoint Content Databases Size – Storage Report using PowerShell

Requirement: Get content database size in SharePoint using PowerShell

How to check SharePoint content database size?
Have you ever wondered how to get the size of a content database in SharePoint? Well, This post will demonstrate how to get the size of a content database in SharePoint using PowerShell. As noted by Microsoft, The maximum recommended size for a content database is 200 GB, This process can be useful in determining if your current content database is within these parameters.

To get the sizes of all your SharePoint content databases, use this PowerShell script:

Get-SPContentDatabase | foreach { $_.Name;[Math]::Round(($_.disksizerequired/1GB),2) }

Fairly simple! isn’t it? But every month I’ve had to generate the Storage report for SharePoint Content Databases. How about automating it with PowerShell? Sure, Here is my PowerShell script to get SharePoint content databases size.

Get SharePoint Content Database Size using PowerShell:

Let’s get the SharePoint content database’s size, export it to a CSV and then email using PowerShell.

#SharePoint Content Database Sizes - Storage Report
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue 

$ReportDate=get-date -Format "MMM-dd-yyyy"

$OutputFN = "D:\Database_Storage_$ReportDate.csv"

#If the File already exists remove it
if (Test-Path "D:\Database_Storage_$ReportDate.csv")
{
    Remove-Item "D:\Database_Storage_$ReportDate.csv"
}

#Write CSV Header
 Add-Content -Path $OutputFN -Value "Web Application Name, URL, Database Name, Database Size-GB"
    
#Get All Web Applications
$webapps = Get-SPWebApplication
foreach($webapp in $webapps)
{
    $ContentDatabases = $webapp.ContentDatabases
    
    foreach($ContentDatabase in $ContentDatabases)
    {
        $ContentDatabaseSize = [Math]::Round(($ContentDatabase.disksizerequired/1GB),2)

        Add-Content -Path $OutputFN " $($webapp.Name),  $($webapp.url) , $($ContentDatabase.Name) , $ContentDatabaseSize"
    }
} 

#Code To mail the Report
$SMTPClient = new-object System.Net.Mail.smtpClient
$SMTPClient.host = "smtp.crescent.org"

$MailMessage = new-object System.Net.Mail.MailMessage
$MailMessage.Subject = "SharePoint Storage Report - $ReportDate"
$MailMessage.Body = "Please find attached Database storage report as on $ReportDate"
$MailMessage.From = "SharePoint-Reports@Crescent.com"
$MailMessage.To.add("Salaudeen.Rajack@Crescent.com")
$Attachment = new-object System.Net.Mail.Attachment($OutputFN)
$MailMessage.Attachments.Add($Attachment)
$SMTPClient.Send($MailMessage)

$Attachment.Dispose();
$MailMessage.Dispose();

#Remove the Report from Server once mailed!
Remove-Item "D:\Database_Storage_$ReportDate.csv"

Output: (After adding a Pivot Table)

sharepoint content database size powershell

Tail: Once you have the script ready, You can place it under Task Scheduler to run the script on a scheduled basis! like: PowerShell.exe D:\Reports\StorageReport.ps1

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

2 thoughts on “SharePoint Content Databases Size – Storage Report using PowerShell

  • Hi, I am running this from my WFE and getting the email ok with attachment, but am getting an error message:
    “The term ‘Get-SPWebApplication’ is not recognized as the name of a cmdlet… ObjectNotFound: (Get-SPWebApplication:String)” in powershell. Do you have any suggestions?

    Running the cmd:
    PS D:SPReports> .contentdbstoragereport.ps1

    Thanks for looking

    Reply
    • Are you trying to Run the script in MOSS 2007? Get-SPWebApplication will work only on SharePoint 2010! To use it in MOSS, Take a Look at Gary’s script: https://blog.falchionconsulting.com/index.php/2009/04/getting-an-spwebapplication-object-using-powershell/

      Reply

Leave a Reply

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