Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Created April 6, 2018 18:29
Show Gist options
  • Select an option

  • Save JohnLBevan/6ff416b0da66215b6a795862c94d2e1c to your computer and use it in GitHub Desktop.

Select an option

Save JohnLBevan/6ff416b0da66215b6a795862c94d2e1c to your computer and use it in GitHub Desktop.
Find everyone a mail's addressed to by loading an MSG file from MS Outlook
function Get-MailRecipients {
[CmdletBinding(DefaultParameterSetName = 'ByFilename')]
Param (
[Parameter(Mandatory = $true, ParameterSetName = 'ByFilename', ValueFromPipeline = $true)]
[string]$Path
,
[Parameter(Mandatory = $false, ParameterSetName = 'ByFilename')]
[PSObject]$Outlook = $null
,
[Parameter(Mandatory = $true, ParameterSetName = 'ByMessage')]
[PSObject]$Message
)
Begin {
$closeOutlook = $false #only close outlook if we openned it
if (($PSCmdlet.ParameterSetName -eq 'ByFilename') -and ($Outlook -eq $null)) {
$Outlook = (New-Object -ComObject 'Outlook.Application')
$closeOutlook = $true
}
}
Process {
if ($PSCmdlet.ParameterSetName -eq 'ByFilename') {
$Message = $Outlook.Session.OpenSharedItem($Path)
}
$Message.Recipients | ForEach-Object {
$_.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x39FE001E") #see https://stackoverflow.com/a/45296809/361842 for mapping between properties & their schemas
}
}
End {
if ($closeOutlook) {
$Outlook.Quit()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment