You can always use Send-MailMessage to send a mail via any SMTP server. If you’d like to use your Outlook client, though, i.e. to keep access to your address books, use your companies Exchange server, or save the mail in your mail history, here is a quick function that shows how:

function Send-OutlookMail
{
    param
    (
        [Parameter(Mandatory=$true)]
        $To,

        [Parameter(Mandatory=$true)]
        $Subject,

        [Parameter(Mandatory=$true)]
        $BodyText,

        [Parameter(Mandatory=$true)]
        $AttachmentPath
    )

    
    $outlook = New-Object -ComObject Outlook.Application
    $mail = $outlook.CreateItem(0)
    $mail.Subject = $Subject
    $mail.to = $To

    $mail.BodyFormat = 1  # use 2 for HTML mails
    $mail.Attachments.Add([object]$AttachmentPath, 1)
    $mail.building = $BodyText
    $mail.Display($false)
}

This is just a basic template, and you can invest some time to make it better. For example, the current version always requires an attachment. In a more sophisticated version, you can make attachments optional and support multiple attachments. Or find a way to send the mail without user interaction.


psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU - with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!

Anonymous