The Outlook Inbox Filter doesn't work....why not?

84 views Asked by At

Instead of showing ReceivedTime only for Inbox items for the last two days, this shows for all Inbox items, why?

$objOutlook = new-object -comobject Outlook.Application
$namespace = $objOutlook.GetNameSpace("MAPI")
$InboxFolder = $namespace.GetDefaultFolder(6)

$DateYest = (Get-Date).AddDays(-2)
$InboxFolder | where-object {$_.ReceivedTime -gt "$DateYest"}
ForEach ($MailItem in $InboxFolder) {
write-host $MailITem.ReceivedTime
}
1

There are 1 answers

1
Doug Maurer On BEST ANSWER

The code you posted doesn't return any inbox items. As written you are filtering your one inbox by it's nonexistent RecievedTime property. My assumption is your example is missing the .Items property. If you add .Items to your filter as it stands it wouldn't be captured to a variable or passed down the pipe so it would output directly to the console. However when written properly the filter works fine.

$objOutlook = new-object -comobject Outlook.Application
$namespace = $objOutlook.GetNameSpace("MAPI")
$InboxFolder = $namespace.GetDefaultFolder(6)

$DateYest = (Get-Date).AddDays(-2)
$filteredItems = $InboxFolder.items | where-object {$_.ReceivedTime -gt $DateYest}
foreach ($MailItem in $filteredItems) {
    write-host $MailITem.ReceivedTime
}

or

$objOutlook = new-object -comobject Outlook.Application
$namespace = $objOutlook.GetNameSpace("MAPI")
$InboxFolder = $namespace.GetDefaultFolder(6)

$DateYest = (Get-Date).AddDays(-2)
$InboxFolder.items |
    Where-Object {$_.ReceivedTime -gt $DateYest} |
        ForEach-Object {
            write-host $_.ReceivedTime
        }