Outlook: deleting mail items

As promised here is the function to delete mail items in a specific folder

function remove-mailitem {            
param (            
if ($start -and $finish){            
  if ($start -ge $finish){Throw "Finish $($finish) before Start $($start)"}            
$outlook = New-Object -ComObject Outlook.Application            
$folders | where {$_.Path -like "*$mailfolder"} |            
foreach {            
$targetfolder = $outlook.Session.GetFolderFromID($_.EntryID, $_.StoreID)            
foreach ($item in $targetfolder.Items)  {            
 if ($start){if ($item.SentOn -lt $start){continue}}            
 if ($finish){if ($item.SentOn -gt $finish){continue}}            
  if ($psCmdlet.ShouldProcess("$($item.Subject) $($item.SentOn)", "deleting")) {            

If you’ve been following this series you will recognise how this works. The parameters are the mail folder and an optional start and finish date. A quick check to see if the dates are the right way round and we jump into the processing. The folders that match our input are selected and foreach of them we loop through the mail items.

The SentOn date is compared to the start and finish dates if they are defined. The item is skipped if it falls outside of those dates.

We can then delete the item. I’ve also added the –whatif parameter to the function by using   [CmdletBinding(SupportsShouldProcess=$true)] and

if ($psCmdlet.ShouldProcess(“$($item.Subject) $($item.SentOn)”, “deleting”)) {

This adds another safety level to the function

This entry was posted in Office 2010, Outlook, PowerShell V2, Windows 7. Bookmark the permalink.

1 Response to Outlook: deleting mail items

  1. Jonathan Hill says:

    I’ve been playing with deleting items in Outlook via powershell a lot recently, and one of the things that I’ve experienced, but no-one else seems to have mentioned is that if you use foreach to iterate through a Items collection and delete each item, it only ever deletes half of the items.
    This is something that I noticed when I first started working with collections in VB6, Iterating through the list and removing an item moves all the other items up one slot, then when you move on to the next item it is effectively moving on two steps.
    i.e. with the collection (1,2,3,4,5,6)
    using a for each $Item in the Collection and deleteing each one would give
    1. get item 1, delete it, move onto the next item
    2. get item 3, delete it, move onto the next item
    3. get item 5, delete it, no more items.
    leaving (2,4,6) in the collection

    The solution is to step through the collection backwards from the last element to the first so that every item is processed in one pass.

    This happens everytime I’ve run anyone elses code to do this thing, everyone is using the same mechanism, but no-one else mentions the skipping problem, am I doing something wrong, or is no-one actually running their code?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s