Scenario: we have a dedicate mailbox that receives a regular report attached to an email every morning.
We want that attachment to be saved to a specific folder on a server, and move the message away from the Inbox to keep it clear.
1. Before We Begin
First, we create a new profile in Outlook called 'RegularReports'.
Then, we create a subfolder inside the Inbox called 'Old Reports', where the processed messages will be moved to.
2. The Script
The following PowerShell script will open the 'RegularReports' Outlook user profile, cycle through all the messages in the Inbox, and saves the *.7z attachments to C:\temp. When it finishes all messages are marked as read, and moved to ':\Inbox\Old Reports' folder in Outlook.
# Kill OUTLOOK.EXE if running in my context, otherwise the script won't run Get-Process OUTLOOK -IncludeUserName | ?{$_.UserName -eq "ALWAYSHOTCAFE\admin"} | Stop-Process -Force # Load Outlook $o = New-Object -comobject outlook.application $o.Session.Logon("RegularReports") $n = $o.GetNamespace("MAPI") # Pick Inbox $Inbox =$n.GetDefaultFolder(6) $SaveToFolder = "C:\temp" $TargetFolder = $Inbox.Folders.Item("Old Reports") $Inbox.Items | % { $msg = $_ # Save attachment $msg.attachments | ?{$_.filename -like "*.7z"} | %{ $file = $_.filename $_.saveasfile((Join-Path $SaveToFolder $file)) } } # Mark the messages as read, and move them to the 'Old Reports' folder. # Because of the imperfection of the move() function, the loop doesn't end until the messages are moved while ($Inbox.Items.Count -gt 0) { foreach ($mail in $Inbox.Items) { $mail.UnRead = $false $mail.Move($TargetFolder) } }
3. Allow the script access to Outlook
You might receive the following prompt, asking you to allow the script access to the mailbox.
If that happens, open Outlook, navigate to 'File/Options/Trust Center'. Locate the 'Programmatic Access' option and select 'Never warn me about suspicious activity'
4. Create a Scheduled Task
Next, we create a scheduled task on the computer to run the script every morning, even if the user is not logged in.
a. Open Task Scheduler, select 'Create Task'. Name will be 'Regular Report download every morning at 6am', make sure to select: run the task whether user is logged in or not.
b. Under the Triggers tab create a new trigger on a schedule, weekly every day at 6am.
c. Going forward on the Actions tab configure to start powershell with the following arguments that specify the script to execute and the executionpolicy to make sure the script is not blocked
Program/script: 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'
Arguments: '-ExecutionPolicy Bypass -File c:\scripts\RegularReports.ps1'
d. Next, under the Conditions tab we don't need to check anything
e. Finally, under the Settings area we allow the task to be run on demand, so we can test it anytime.
f. When running the task, don't worry if you see 0x41301 under run result, that means the script is still running. When it finishes the result will show 0x0
Henrique da Costa says
Your script is sensational, but help me if you can. I receive several emails with XML and PDF files. I extract only the XMLs, but while the script is running and moving the messages to the Processed folder, other new incoming messages are not handled…