Jeremy Taylor

SharePoint & more

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: http://www.toddklindt.com/blog/Lists/Posts/Post.aspx?ID=270

Secondly, thanks to Harold Odgen for explaining on how to Send an email with an attachment using PowerShell: http://www.techrepublic.com/blog/window-on-windows/send-an-email-with-an-attachment-using-powershell/4969

 # 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 = “127.0.0.1″  #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 = “youradmin@yourcompany.com” #Change this to your desired from email address
$msg.To.Add(“your@yourcompany.com”) #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”
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()

,

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>