Last active
August 16, 2018 05:53
-
-
Save rdapaz/cb5015f9a282f7a5ae6f66be2f6388bd to your computer and use it in GitHub Desktop.
Email Attachment Finder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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