kwizcom banner advertisement

SharePoint Content Databases Size - Storage Report using PowerShell

How to check SharePoint content database size?
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:
Lets get SharePoint content databases size, Export 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 scheduled basis! like: PowerShell.exe D:\Reports\StorageReport.ps1
SharePoint Content Databases Size - Storage Report using PowerShell SharePoint Content Databases Size - Storage Report using PowerShell Reviewed by Salaudeen Rajack on May 11, 2012 Rating: 5

2 comments:

  1. 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

    ReplyDelete
    Replies
    1. 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: http://blog.falchionconsulting.com/index.php/2009/04/getting-an-spwebapplication-object-using-powershell/

      Delete

Please Login and comment to get your questions answered!

Powered by Blogger.