Categories

Emptying the Deleted folders–version 2

The original version of this function only did one pass at deleting and had to iterate through all of the folders to find the Deleted Items folder.  This time we use the collection of folders we created using the get-mailfolders function to go directly to the Deleted Items folders

function clear-deletedmail {            
            
$outlook = New-Object -ComObject Outlook.Application            
            
get-mailitemcount -deleted            
            
$folders | where {$_.Path -like "*deleted*"} |            
foreach {             
  $mailfolder = $outlook.Session.GetFolderFromID($_.EntryID, $_.StoreID)            
              
  if ($mailfolder.Items.Count -gt 0){            
    do {            
      foreach ($item in $mailfolder.Items){$item.Delete()}            
    } while ($mailfolder.Items.Count -gt 0)            
  }               
}            
get-mailitemcount -deleted            
}


The function starts by creating an Outlook object. The get-mailitemcount function has been revised to provide just the deleted or junk folders (I’ll post about the revisions next)



The folders collection is filtered to select just the Deleted Items folders (I’m assuming the word deleted hasn’t been used in any other folder names).  Each of the Deleted Items folders is accessed using the GetFoderFromID function using the EntryID and StoreID saved in the objects in the folder. If the number of items in the folder is not zero we create a do loop to control the deletion. This will keep calling the foreach loop that performs the deletion until everything is deleted (remember our original function need several passes to delete all the items in the folder)



A final call to get-mailitemcount shows the position after all of the items are deleted.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>