Clearing junk mail

Getting back to looking at working with Outlook we can adapt the function used to deleted the contents of the Deleted Items folder to work with the Junk mail folders

function clear-junkmail {            
            
$outlook = New-Object -ComObject Outlook.Application            
            
get-mailitemcount -junk            
            
$folders | where {$_.Path -like "*junk*"} |            
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 -junk            
}

Very much the same as before but we are looking for folders that contain the work “junk”

It would be possible to combine this function and the clear-deletedmail function but I decided to keep them separate for simplicity

Published Tue, Aug 2 2011 19:42 by RichardSiddaway

Comments

# re: Clearing junk mail

You should really check out the GetDefaultFolder method so you don't have to loop through all the folders.  

Tuesday, August 02, 2011 3:01 PM by Robert

# re: Clearing junk mail

The GetDefaultFolder method only returns the folders for the mailbox that is designated as the primary - usually the first one you connect to.

The whole point of these posts is dealing with multiple mailboxes which means you have to take a slightly different approach as I pointed out in an earlier post

Wednesday, August 03, 2011 3:20 AM by RichardSiddaway

# re: Clearing junk mail

What about if you created a custom folder, like for a certain person, and you wanted to do the same with that?  How would that work?  Also what about a pst backup?

Wednesday, August 03, 2011 9:01 AM by jcwfbi

# re: Clearing junk mail

This post msmvps.com/.../outlook-deleting-mail-items.aspx

shows how to delete the mail items in a particular folder

Thursday, August 04, 2011 11:18 AM by RichardSiddaway

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: