Monthly Archive


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

4 Responses to Clearing junk mail

Leave a Reply