Delete emails from Outlook

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"} 
 

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

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

foreach ($mail in $JunkMail) {
    $mail.Delete()
}

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.

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.

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()
}


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:

http://www.slipstick.com/outlook/config/how-to-automatically-empty-outlooks-deleted-item-folder/

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

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