A few days ago, I had to find a way to automatically send an email with attachments, at a predetermined time, using Outlook and PowerShell. I decided to use a Windows Scheduled Task, that would wake up at that time and invoke a PowerShell script.

The script below is the result of a collection of snippets and bits of code I found in various places around the Internet that allowed me to solve this problem.

For this example, I only want to send the files which have a specific extension (.html) and that exist in a specific folder (passed as parameter to the PowerShell script). Of course, you are free to remove this extension filtering for your own purposes.

Also, note that Outlook must have a configured email account for the sending process to succeed.

# Check to see we have all the arguments
if($args.Count -lt 1)
{
Write-Host "Use: SendMail.ps1 <Path>"
Write-Host
Write-Host " <Path>: Full path for the folder which contains the files"
Write-Host
exit
}

$FullPath=$args[0]

#Get an Outlook application object

$o = New-Object -com Outlook.Application

$mail = $o.CreateItem(0)

#2 = High importance message
$mail.importance = 2

$mail.subject = "This is the subject of the mail"
$mail.body = "This is the body of the email. It has been automatically generated by a script."

#separate multiple recipients with a ";"
$mail.To = <INSERT THE RECIPIENT HERE>
#$mail.CC = <OTHER RECIPIENT 1>;<OTHER RECIPIENT 2>

# Iterate over all files and only add the ones that have an .html extension
$files = Get-ChildItem $FullPath

for ($i=0; $i -lt $files.Count; $i++) {

$outfileName = $files[$i].FullName
$outfileNameExtension = $files[$i].Extension

# if the extension is the one we want, add to attachments
if($outfileNameExtension -eq ".html")
{
$mail.Attachments.Add($outfileName);
}
}

$mail.Send()

# give time to send the email
Start-Sleep 20

# quit Outlook
$o.Quit()

#end the script
exit

You can invoke the script presented above, by using a simple command window and giving the following command:

PowerShell Command

I hope this helps!

6 COMMENTS

  1. Hi Silva,
    Your post is helpful thanks for sharing

    My scenario is Reply to an existing outlook email with attachments using power shell.

    Could you please help me in this

    Regards,
    Karthik

  2. Hello Karthik.

    Where is this existing Outlook message? Is it a .msg file in the file system? Or is it in an Outlook inbox?

    Thanks,
    André.

  3. Thank you so much for publishing this article. It works like a charm. I was trying to see a nice way and avoiding the need to write a VB script and embed it in Outlook for automation. This one does it.

  4. I’m doing something similar, though just sending a basic e-mail and running into an issue with Outlook getting stuck half-open in the system tray. It won’t open at this point without exiting or killing the process. Just wondering if you’d seen this?
    I’ve tried multiple other variations including the following after the quit but nothing has helped.

    Start-Sleep 30
    while( [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook)){‘released one count’}
    Remove-Variable Outlook
    [System.GC]::Collect()

LEAVE A REPLY

Please enter your comment!
Please enter your name here