Delete emails from Outlook

This topic contains 7 replies, has 3 voices, and was last updated by  H Man 2 years, 4 months ago.

  • Author
    Posts
  • #24956

    H Man
    Participant

    Hello I am trying to automate the deletion of emails from my junk emial folder

    heres what i have so far , no errors but just not working

    #Shutdown Outlook
    if  ( [bool](Get-Process OUTLOOK* -EA SilentlyContinue) ) {ps OUTLOOK* | kill -Force   ; Start-Sleep -Seconds 10  } 
    
    #Create Time Filter
    $Date = [DateTime]::Now.AddDays(-2)
    $TimeFilter =  $Date.tostring("MM/dd/yyyy")
    
    $o = New-Object -comobject outlook.application
    $n = $o.GetNamespace(“MAPI”)
    
     
    $Account = $n.Folders | ? { $_.Name -eq 'halexander@YAHOO.com' };
    $JunkEmail = $Account.Folders | ? { $_.Name -match 'Junk Email' };
    
    
    For($i=($JunkEmail.count-1);$i -ge 0;$i--){
        $($JunkEmail)[$i].Unread = $false
        $($JunkEmail)[$i].delete()
    }
    
    #Shutdown Outlook
    if  ( [bool](Get-Process OUTLOOK* -EA SilentlyContinue) ) {ps OUTLOOK* | kill -Force   ; Start-Sleep -Seconds 5  } 
    #Start Outlook
    if   (!( [bool](Get-Process OUTLOOK -EA SilentlyContinue) )) {ii "C:\Program Files\Microsoft Office\Office14\OUTLOOK.EXE"} 
     
    
    
  • #24964

    Jack Neff
    Participant

    Take another look at what you are cycling through in your for loop.

  • #24966

    Rob Simmers
    Participant

    You could try a simpler FOR logic too instead of referencing it by index:

    foreach ($mail in $JunkMail) {
        $mail.Delete()
    }
    
  • #24967

    Jack Neff
    Participant

    Rob's right your loop is a little over complicated BUT I think it may have actually saved you from deleting your Junk Email folder in this case. I also realized I should have been more specific with my last post.

    As written, $JunkEmail does not contain email objects (class 43). It contains a folder (class 2). Try expanding $JunkEmail.items or re-write how you populate the variable. Since $JunkEmail only contains 1 item (the Junk Email folder itself) and your loop goes to itemcount – 1, the loop never actually iterates and therefore you do not call method delete on your Junk Email folder..haha. Probably not a big deal in this case since it was your junk folder but if it were inbox or something, might have been a bit more exciting.

  • #24968

    Jack Neff
    Participant

    Never mind, it should have iterated once but since $JunkEmail isn't an array it doesn't have indices and didn't call the delete. IDK, anyway you get it.

  • #24969

    H Man
    Participant

    Hi Guys thx for the response I am doing this now and still no luck

    $Junk = $Account.Folders | ? { $_.Name -match 'Junk Email' };
    
    
    $emails = $junk.Items
    
    
    
    Foreach($e in $emails){
    $e.delete()
    }
    
    
    
  • #24970

    Rob Simmers
    Participant

    As Jeff was saying, your trying to delete nothing. If you enumerate $emails, is there anything even in it. You need to make sure your query is returning objects and specifically mail objects, not folder objects.

    $Junk = $Account.Folders | ? { $_.Name -match 'Junk Email' };
    $emails = $junk.Items
    $emails #You should see the emails you are expecting to delete, if not you need to refine your query
    

    You can configure Outlook client to delete items if they are X days old or when you exit the client according to this:

    This script show how you would count the items in the Junk folder, so if there is a Delete() method this would get there:

    http://blogs.technet.com/b/heyscriptingguy/archive/2009/01/26/how-do-i-use-windows-powershell-to-work-with-junk-e-mail-in-office-outlook.aspx

  • #24971

    H Man
    Participant

    I am seeing the emails , thx Rob for all your help not sure why they wont delete

You must be logged in to reply to this topic.