title: enumerate and email with powershell
body: <p>ok fellas, I've been reading, researching, learning and testing about powershell. Within the last 20 days, here is what I've been able to come up with.</p>
  
  <pre><code>get-mailbox -identity $_.name | select name,userprincipalname,@{label="Size";expression={$size = Get-Mailboxstatistics $_.identity;$size.TotalItemSize.Value.ToMB()}},@{label="Items";expression={$items = Get-Mailboxstatistics $_.name;$item.ItemCount}}
  </code></pre>
  
  <p>I stored this in a script called accountsizes.ps1. It works exactly as I expected by outputting all the email accounts with the sizes, but in order for me to get only the mailboxes over 2048MB, I have to call it like this:</p>
  
  <pre><code>PS C:\accountsizes.ps1 | where size -gt "2048" | select userprincipalname,size
  </code></pre>
  
  <p>And this works by return the email addresses and mailbox sizes in MBs. But now my dilemma is; how do I enumerate through the results and extract each email address and send an email to that user and myself, warning them that their mailbox is too large and they need to archive. From what I been reading and learning, I would have to use a ForEach loop and the send-mailmessage cmdlet. I cannot figure out how to use the ForEach and incorporate it with the script: Here is I go brain dead with the ForEach:</p>
  
  <pre><code>PS C:\accountsizes.ps1 | where size -gt "2048" | select userprincipalname,size | ForEach($user in userprincipalname){$_.userprincipalname}
  </code></pre>
  
  <p>I do not know the right way to go about doing this (so, don't ask me why I'm doing this way :)), I have no previous knowledge about scripting and coding.</p>
  
  <p>Here is my email part:</p>
  
  <pre><code>$smtpserver = "domain.com"
  $smtpFrom = "me@domain.com"
  $smtpTo = "you@domain.com"
  $messageSubject = "Warning Email Mailbox Too Large"
  $body = "blah blah blah blah"
  
  send-mailmessage -from $smtpFrom -to $smtpTo -subject $messageSubject -body $body
  </code></pre>
  
  <p>Thanks in advance for your helpful advice.</p>