How to Identify the Size of the Content Database using PowerShell in SharePoint 2013

Sathish Nadarajan
 
Solution Architect
January 12, 2015
 
Rate this article
 
Views
23172

Recently I came across a requirement like retrieving the Content DB Size of a WebApplication using PowerShell. The WebApplication may have many Content DBs. So, all the Content DBs needs to be listed and their corresponding sizes needs to be exported into an Excel as output.

Thought of Sharing to the community.

 $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
 
 $LogFile = ".ContentDBSize-$LogTime.rtf"
 
 #cls
 
 ##================================================================================================
 
 ## Description : Get the Content DBs and their Sizes
 
 ## Author : Sathish Nadarajan
 
 ## Date : 09-Jan-2015
 
 ##================================================================================================
 
 $Host.UI.RawUI.WindowTitle = "-- Get Content DBs Size --"
 
 $StartDate = Get-Date
 
 Write-Host -ForegroundColor White "------------------------------------"
 
 Write-Host -ForegroundColor White "| Get Content DBs Size |"
 
 Write-Host -ForegroundColor White "| Started on: $StartDate |"
 
 Write-Host -ForegroundColor White "------------------------------------"
 
 #start-transcript $logfile
 
 ################################################### Add SharePoint PowerShell Snapin ######################################
 
 if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
 
 Add-PSSnapin Microsoft.SharePoint.Powershell
 
 }
 
 ########################################################### End of PowerShell Snapin ########################################
 
 ########################################################### Set the Exxecution Path #########################################
 
 $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
 
 Set-Location $scriptBase
 
 ########################################################### End of Set the Exxecution Path ##################################
 
 ########################################################### Prepare the Output File with Header #############################
 
 $Output = $scriptBase + "" + "ContentDBInfo.csv";
 
 "WebApplication" + "," + "ContentDBName" + "," + "Size in MB" | Out-File -Encoding Default -FilePath $Output;
 
 ############################################### End of Prepare the Output File with Header ##################################
 
 $webApplication = Read-Host "Please enter the WebApplication URL"
 
 $contentDataBases = Get-SPContentDatabase -webapplication $webApplication
 
 Write-Host "The Count of Content Databases for the WebApplication is :- " $contentDataBases.Count -ForegroundColor Green
 
 if($contentDataBases.Count -ne $null)
 
 {
 
 for($i=0; $i -le $contentDataBases.Count-1; $i++)
 
 {
 
 Write-Host $contentDataBases[$i].Name
 
 $contentDataBase = Get-SPDatabase | where-object { $_.Name -eq $contentDataBases[$i].Name }
 
 $webApplication + "," + $contentDataBase.Name + "," + $contentDataBase.disksizerequired/1024/1024 | Out-File -Encoding Default -Append -FilePath $Output;
 
 }
 
 }
 

Happy Coding.

Sathish Nadarajan.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment