Skip to content

Instantly share code, notes, and snippets.

@abstrask
Last active January 15, 2019 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abstrask/0d3a9a6a7336536dfdf3d3b24192bd61 to your computer and use it in GitHub Desktop.
Save abstrask/0d3a9a6a7336536dfdf3d3b24192bd61 to your computer and use it in GitHub Desktop.
Get email items from shared Exchange mailbox through Outlook. Can be modified to also output body (and other properties) of messages.
<#
https://msdn.microsoft.com/en-us/magazine/dn189202.aspx
https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/26/use-powershell-to-data-mine-your-outlook-inbox/
#>
Param (
[Parameter(Mandatory=$true)]
[string]$SharedMailboxEmailAddress,
[Parameter(Mandatory=$false)]
[string]$Subfolder
)
# Define constants
$PR_TRANSPORT_MESSAGE_HEADERS = 'http://schemas.microsoft.com/mapi/proptag/0x007D001E'
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
# Open shared mailbox
$recipient = $namespace.CreateRecipient($SharedMailboxEmailAddress)
$recipient.Resolve | Out-Null
If ($Subfolder) {
$MailboxFolder = $namespace.GetSharedDefaultFolder($recipient,
[Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox).Folders[$Subfolder]
} Else {
$MailboxFolder = $namespace.GetSharedDefaultFolder($recipient,
[Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
}
# Parse mail items
$Items = $MailboxFolder.Items | Select ReceivedTime, Subject,
@{N='SenderEmailAddress'; E={If ($_.SenderEmailAddress -like '*@*') {$_.SenderEmailAddress} Else {$_.SenderName}}},
@{N='ToEmailAddress'; E={($_.PropertyAccessor.GetProperty($PR_TRANSPORT_MESSAGE_HEADERS) -split "`n" |
Select-String '^To:\s+(.*)' | ForEach {$_.Matches.Groups[1].Value}) -split ',' |
Select-String '<(.*@.*)>' | ForEach {$_.Matches.Groups[1].Value}}},
@{N='CCEmailAddress'; E={($_.PropertyAccessor.GetProperty($PR_TRANSPORT_MESSAGE_HEADERS) -split "`n" |
Select-String '^CC:\s+(.*)' | ForEach {$_.Matches.Groups[1].Value}) -split ',' |
Select-String '<(.*@.*)>' | ForEach {$_.Matches.Groups[1].Value}}}
# Return
Return $Items
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment