Powershell Script to Generate Report for SharePoint Content Database

The script gets you the below details about the SharePoint content database,

  1. Content database located server
  2. CDB status
  3. CDB size
  4. Site Level warning
  5. Maximum allowed sites
  6. Total site collection
  7. Site collection URL
  8. Subsite count
  9. Site collection size

  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