Skip to main content

Comprehensive Computer Solutions, LLC

Maximizing the return on your IT investment

Home  Services  What's New  PowerShell Corner  About Us  Site Map  Contact Us  Member Login   
PowerShell Corner
 
Here's where short but useful snippets of PowerShell code will be shared with the world at large. As we come up with potentially generally useful snippets, we'll add them here for others to leverage in their own environments.

Send an email with an attachment
 
This ridiculously simple one-liner from PowerShell highlights a key element of any automation script, forwarding on pertinent data to a central administrative mailbox.
 
Send-MailMessage [-To] <String[]> [-Subject] <String> [[-Body] <String>] [[-SmtpServer] <String>] `
  [-Attachments <String[]>] [-Bcc <String[]>] [-BodyAsHtml] [-Cc <String[]>] [-Credential <PSCredential>] `
  [-DeliveryNotificationOption <DeliveryNotificationOptions>] [-Encoding <Encoding>] [-Port <Int32>] `
  [-Priority <MailPriority>] [-UseSsl] -From <String> [<CommonParameters>]
 
Example:
 
Send-MailMessage -To "administrator@somewhere.com" -Subject "Sample email message" -Body "This is a sample message sent with
  PowerShell" -SmtpServer "mailserver@somewhere.com" -Attachments "C:\Logs\SampleLog.txt" -From "PowerShellScript@somewhere.com"