JBird
JBird

Reputation: 11

How to change script to save attachments by file name instead of by sender name?

I'm using the script below. It works well to save attachments by sender name in a specified folder. However, if the sender names are constant, it only saves 1 of the attachments vs. all of the attachments. I'm assuming it's a write error. How do I update the script below to save all attachments meeting the filtered criteria by their actual attachment name instead of sender name.

$o = New-Object -comobject outlook.application
$n = $o.GetNamespace("MAPI")
$f = $n.PickFolder()
$filepath = "c:\test"

$f.Items| foreach {$SendName = $_.Sendername
$_.attachments|foreach {
$_.filename
If ($_.filename.Contains("pdf")) {
$_.saveasfile((Join-Path $filepath "$SendName.pdf"))}}}`

Any ideas would be greatly appreciated.

Upvotes: 1

Views: 478

Answers (1)

ArcSet
ArcSet

Reputation: 6860

So Lets Follow the rabbit hole here.

We can go to the Outlook Object Model, and look for Attachments Object because we see you are iterating over attachments:

$_.attachments|foreach

We see in the page:

Contains a set of Attachment objects

So we go look at the Attachment Object Page, look at Properties and we see there is a property for FileName

So to send by attachment name we can do this:

$o = New-Object -comobject outlook.application
$n = $o.GetNamespace("MAPI")
$f = $n.PickFolder()
$filepath = "c:\test"

$f.Items| foreach {
    $FileName= $_.FileName
    $_.attachments|foreach {
        $_.filename
        If ($_.filename.Contains("pdf")) {
            $_.saveasfile((Join-Path $filepath "$FileName"))
        }
    }
}

Upvotes: 2

Related Questions