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. $CDBName = read-host "Enter the content database name "
  4. write-host "Generating report for the Content database " $CDBName -fore yellow
  5. write-host "Processing report..................." -fore magenta
  6. $Output = $scriptBase + "\" + "03ContentDBDetails.csv";
  7. "CDBName" + "," + "CDBServer" + "," + "CDBStatus" + "," + "CDBSize(MB)" + "," + "SiteLevelWarning" + "," + "MaximumAllowedSites" + "," + "TotalSiteCollection" + "," + "SiteCollectionURL" + "," + "Web(s)Count" + "," + "SiteCollectionSize(MB)" | Out-File -Encoding Default -FilePath $Output;
  8. $CDB = get-spcontentdatabase -identity $CDBName
  9. $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;
  10. $sites = get-spsite -limit all -ContentDatabase $CDBName
  11. foreach($site in $sites)
  12. {
  13. $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $site.url + "," + $site.allwebs.count + "," + $site.usage.storage/1048576 | Out-File -Encoding Default -Append -FilePath $Output;
  14. }
  15. write-host "Report collected for content database " $CDBName " and you can find it in the location " $output -fore green
  16. }
Complete Code
  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
  2. $LogFile = ".\ContentDatabaseReportPatch-$LogTime.rtf"
  3. # Add SharePoint PowerShell Snapin
  4. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
  5. Add-PSSnapin Microsoft.SharePoint.Powershell
  6. }
  7. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
  8. Set-Location $scriptBase
  9. #Deleting any .rtf files in the scriptbase location
  10. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
  11. if($FindRTFFile)
  12. {
  13. foreach($file in $FindRTFFile)
  14. {
  15. remove-item $file
  16. }
  17. }
  18. start-transcript $logfile
  19. Function ContentDatabaseReport()
  20. {
  21. $CDBName = read-host "Enter the content database name "
  22. write-host "Generating report for the Content database " $CDBName -fore yellow
  23. write-host "Processing report..................." -fore magenta
  24. $Output = $scriptBase + "\" + "03ContentDBDetails.csv";
  25. "CDBName" + "," + "CDBServer" + "," + "CDBStatus" + "," + "CDBSize(MB)" + "," + "SiteLevelWarning" + "," + "MaximumAllowedSites" + "," + "TotalSiteCollection" + "," + "SiteCollectionURL" + "," + "Web(s)Count" + "," + "SiteCollectionSize(MB)" | Out-File -Encoding Default -FilePath $Output;
  26. $CDB = get-spcontentdatabase -identity $CDBName
  27. $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;
  28. $sites = get-spsite -limit all -ContentDatabase $CDBName
  29. foreach($site in $sites)
  30. {
  31. $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $empty + "," + $site.url + "," + $site.allwebs.count + "," + $site.usage.storage/1048576 | Out-File -Encoding Default -Append -FilePath $Output;
  32. }
  33. write-host "Report collected for content database " $CDBName " and you can find it in the location " $output -fore green
  34. }
  35. ContentDatabaseReport
  36. write-host ""
  37. write-host "SCRIPT COMPLETED" -fore green
  38. 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.