Skip to content

Instantly share code, notes, and snippets.

@rdapaz
Last active August 16, 2018 05:53
Show Gist options
  • Save rdapaz/cb5015f9a282f7a5ae6f66be2f6388bd to your computer and use it in GitHub Desktop.
Save rdapaz/cb5015f9a282f7a5ae6f66be2f6388bd to your computer and use it in GitHub Desktop.
Email Attachment Finder
# These get modified for each run
$dict = @{
"xls" = "Excel";
"mpp" = "Project Plans";
"ppt" = "Slide Decks";
"vsd" = "Visio";
"doc" = "Word";
"pdf" = "PDF"
};
$path = "C:\Users\rdapaz\Dropbox\Projects\Next Level Email";
foreach ($pair in $dict.GetEnumerator()) {
$filetype = $pair.key
$folder = $pair.value
# Initialise stuff
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
# Do stuff
# Folders.Item(2).Folders.Item(2) is Inbox on the second email account
$inbox = $namespace.Folders.Item(1).Folders.Item(2)
$f = $inbox
# Fixme below
$filepath = Join-Path $path $folder
$f.Items| foreach {
$SendName = $_.SenderName
$_.attachments| foreach {
$newFileName = $SendName + " - " + $_.filename
if ($_.filename -and ($_.filename).Contains($filetype)) {
Write-Host $_.filename
$_.saveasfile((Join-Path $filepath $newFileName))
}
}
}
# Folders.Item(2).Folders.Item(4) is Sent Items on the second email account
$sent_items = $namespace.Folders.Item(1).Folders.Item(4)
$f = $sent_items
$filepath = Join-Path $path $folder
$f.Items| foreach {
$SendName = $_.SenderName
$_.attachments| foreach {
$newFileName = $SendName + " - " + $_.filename
if ($_.filename -and ($_.filename).Contains($filetype)) {
Write-Host $_.filename
$_.saveasfile((Join-Path $filepath $newFileName))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment