Cesar
Cesar

Reputation: 617

Get email Attachment from today only

I would like to get a .txt file from my outlook email that has the current date. so far it will grab anything that has a '.txt' atachment. How would I format it so it gets the email sent from today's date ONLY.

I will be receiving 1 daily email in this folder, so I want to grab that daily file's attachment and place it in folder in my directory. So far I have this:

$i=1

#set outlook to open
$o = New-Object -comobject outlook.application
$n = $o.GetNamespace(“MAPI”)


$f = $n.pickfolder('EDI')


$filepath = “C:\users\cesar.sanchez\desktop\EDI Statement Extract”

$date = get-date (Get-Date).AddDays(-1) -format "MMMM dd"


$f.Items | foreach {
$i=$i+1
    $_.attachments | foreach {
    Write-Host $_.filename
    $a = $_.filename
    If ($a.Contains(“.txt”)) {
    $_.saveasfile((Join-Path $filepath “Nassau EDI ExtractTest _$date.txt”))
      }
  }
}

Upvotes: 0

Views: 148

Answers (1)

Junaid
Junaid

Reputation: 603

Below will go through the inbox and get all text files from emails received today.

$ol = New-Object -ComObject outlook.application
$n = $ol.getNameSpace("MAPI")
$fold = $n.GetDefaultFolder(6).items
$fold |Where {$_.ReceivedTime.Day -eq (Get-Date -format "dd") -AND $_.ReceivedTime.Month -eq (Get-Date -format "MM") -AND $_.ReceivedTime.Year -eq (Get-Date -format "yyyy")  | foreach{
    $_.Attachment | foreach{
        $att = $_
        $_.FileName | Where {$_ -CMatch '\.txt$'} | foreach{
            $att.saveasfile("C:\$_")
        }
    }
}

Hope this helps. Your question certainly did teach me something new.

Upvotes: 2

Related Questions