Content database report in SharePoint

SharePoint Content Database

A content database is a database file that stores content for one or more site collections for SharePoint web application. The content can be pages, files, documents, images and much more. So if the Site Collection has more number of SharePoint sites, the content database size grows rapidly.

The script gets you the below details about the SharePoint content database,
  • Content database located server
  • CDB status
  • CDB size
  • Site Level warning
  • Maximum allowed sites
  • Total site collection
  • Site collection URL
  • Subsite count
  • Site collection size
Below piece of code generates the above said information’s,
  1. Function ContentDatabaseReport()  
  2. {  
  3.   
  4.     $CDBName = read-host "Enter the content database name "  
  5.     write-host "Generating report for the Content database " $CDBName -fore yellow  
  6.     write-host "Processing report..................." -fore magenta  
  7.     $Output = $scriptBase + "\" + "03ContentDBDetails.csv";  
  8.     "CDBName" + "," + "CDBServer" + "," + "CDBStatus" + "," + "CDBSize(MB)" + "," + "SiteLevelWarning" + "," + "MaximumAllowedSites" + "," + "TotalSiteCollection" + "," + "SiteCollectionURL" + "," + "Web(s)Count" + "," + "SiteCollectionSize(MB)" | Out-File -Encoding Default -FilePath $Output;  
  9.     $CDB = get-spcontentdatabase -identity $CDBName  
  10.     $CDB.name + "," + $CDB.server + "," + $CDB.Status + "," + $CDB.DiskSizeRequired/1048576 + "," + $CDB.WarningSiteCount + "," + $CDB.MaximumSiteCount + "," + $CDB.Currentsitecount + "," + $empty + "," + $empty + "," + $empty  | Out-File -Encoding Default  -Append -FilePath $Output;  
  11.     $sites = get-spsite -limit all -ContentDatabase $CDBName  
  12.     foreach($site in $sites)  
  13.     {  
  14.         $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $site.url + "," + $site.allwebs.count + "," + $site.usage.storage/1048576 | Out-File -Encoding Default  -Append -FilePath $Output;  
  15.     }  
  16.     write-host "Report collected for content database " $CDBName " and you can find it in the location " $output -fore green  
  17.   
  18. }  
Complete Code 
  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm  
  2. $LogFile = ".\ContentDatabaseReportPatch-$LogTime.rtf"  
  3.  
  4. # Add SharePoint PowerShell Snapin  
  5.   
  6.   
  7. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {  
  8.     Add-PSSnapin Microsoft.SharePoint.Powershell  
  9. }  
  10.   
  11. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent  
  12. Set-Location $scriptBase  
  13.  
  14.  
  15. #Deleting any .rtf files in the scriptbase location  
  16. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf  
  17. if($FindRTFFile)  
  18. {  
  19.     foreach($file in $FindRTFFile)  
  20.         {  
  21.             remove-item $file  
  22.         }  
  23. }  
  24.   
  25.   
  26. start-transcript $logfile  
  27.   
  28. Function ContentDatabaseReport()  
  29. {  
  30.   
  31.     $CDBName = read-host "Enter the content database name "  
  32.     write-host "Generating report for the Content database " $CDBName -fore yellow  
  33.     write-host "Processing report..................." -fore magenta  
  34.     $Output = $scriptBase + "\" + "03ContentDBDetails.csv";  
  35.     "CDBName" + "," + "CDBServer" + "," + "CDBStatus" + "," + "CDBSize(MB)" + "," + "SiteLevelWarning" + "," + "MaximumAllowedSites" + "," + "TotalSiteCollection" + "," + "SiteCollectionURL" + "," + "Web(s)Count" + "," + "SiteCollectionSize(MB)" | Out-File -Encoding Default -FilePath $Output;  
  36.     $CDB = get-spcontentdatabase -identity $CDBName  
  37.     $CDB.name + "," + $CDB.server + "," + $CDB.Status + "," + $CDB.DiskSizeRequired/1048576 + "," + $CDB.WarningSiteCount + "," + $CDB.MaximumSiteCount + "," + $CDB.Currentsitecount + "," + $empty + "," + $empty + "," + $empty  | Out-File -Encoding Default  -Append -FilePath $Output;  
  38.     $sites = get-spsite -limit all -ContentDatabase $CDBName  
  39.     foreach($site in $sites)  
  40.     {  
  41.         $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $site.url + "," + $site.allwebs.count + "," + $site.usage.storage/1048576 | Out-File -Encoding Default  -Append -FilePath $Output;  
  42.     }  
  43.     write-host "Report collected for content database " $CDBName " and you can find it in the location " $output -fore green  
  44.   
  45. }  
  46.   
  47. ContentDatabaseReport  
  48.   
  49. write-host ""  
  50. write-host "SCRIPT COMPLETED" -fore green  
  51.   
  52. stop-transcript   
Execution Steps
  1. Download and copy the script to the SharePoint server
  2. Launch the SharePoint management shell
  3. Navigate to the script path and execute the script,


Conclusion

Thus this article explains on how to get report of a SharePoint content database using powershell script.
Next Recommended Reading Create Content Type Using PowerShell