Author Posts

May 5, 2015 at 4:22 am

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 '' };
$JunkEmail = $Account.Folders | ? { $_.Name -match 'Junk Email' };

For($i=($JunkEmail.count-1);$i -ge 0;$i--){
    $($JunkEmail)[$i].Unread = $false

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

May 5, 2015 at 7:06 am

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

May 5, 2015 at 7:34 am

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

foreach ($mail in $JunkMail) {

May 5, 2015 at 8:35 am

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.

May 5, 2015 at 9:02 am

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.

May 5, 2015 at 9:31 am

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){

May 5, 2015 at 10:15 am

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:

Automatically Cleanup Outlook's Deleted Item and Junk Email Folders

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

May 5, 2015 at 10:47 am

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