SharePoint 2010 Site Collection size and email it via PowerShell script

I was asked to generate a list of SharePoint sites and their sizes via powershell and notify certain people via email. Here is some powershell that generates a html file with all the site collections in your farm and the sizes in MB and then emails it off to different people.

Unfortunately, at this point in time, there is no powershell to get sizes for ‘subsites’ (SP-Web), only for site collections.

Firstly, thanks to Todd Klindt for his blog post and all the comments on it which got me on track and got my script going:

Secondly, thanks to Harold Odgen for explaining on how to Send an email with an attachment using PowerShell:

 # Generate the Site Collections size html file. Make sure you update the file path to your environment (Set-Content)

$SiteCollurl = @{Expression={$_.url};Label=”Site Collection URL”}
$size = @{Expression={[math]::round($_.diskused/1MB, 2)};label=”Size in MB”}
$1stcontact = @{Expression={$_.OwnerLoginName};Label=”Primary Contact”}
$2ndcontact = @{Expression={$_.SecondaryContactLoginName};Label=”Secondary Contact”}

Get-SPSiteAdministration -Limit All | select $SiteCollurl, $size, $1stcontact, $2ndcontact | Sort-Object -Descending -Property “Size in MB” | ConvertTo-Html -title “Site Collections sorted by size” | Set-Content D:\Scripts\sitecollectionsizes.html

# Email the file. Make sure you update the following to your enviroment

Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
$file = “D:\Scripts\sitecollectionsizes.html”
$smtpServer = “”  #Change this to your SMTP server IP / Name
$att = new-object Net.Mail.Attachment($file)
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = “” #Change this to your desired from email address
$msg.To.Add(“”) #Change this to your email address.
#You can add multiple email addresses by adding new msg.To.Add lines
$msg.Subject = “SharePoint Site Collection sizes”
$msg.Body = “Attached is the SharePoint Site Collection sizes”

Leave a Reply

Your email address will not be published. Required fields are marked *