Export-OutlookMessage.ps1
function Export-OutlookMessage { <# .SYNOPSIS OutlookConnector function: Saves Outlook message to a file on disk. .DESCRIPTION Saves one or messages to a file on disk at specified path. All messages are saved in same folder, and file names are built based on customizable parameter FileNameFormat. Messages can be obtained by one of Get-Outlook commands. Messages are saved in MSG format. If message with same name exists, numbering will be added at the end of file name. .EXAMPLE (Get-OutlookInbox | sort receivedtime -Descending)[0] | Export-OutlookMessage -OutputFolder 'C:\tmp' Save last message from Inbox to C:\tmp folder .EXAMPLE Get-OutlookMessage Inbox | ? Sendername -Match 'boss' | Export-OutlookMessage -OutputFolder 'C:\boss' Get-OutlookMessage SentMail | ? To -Match 'boss' | Export-OutlookMessage -OutputFolder 'C:\boss' Save your entire communication with boss to a folder on a disk. .PARAMETER Messages Mandatory parameter which is content of one or more messages that will be saved to disk. Messages can be obtained with one of Get-Outlook commands. Type Get-Command Get-Outlook* for list of commands. Messages can be provided either as parameter, or via pipeline. .PARAMETER OutputFolder Mandatory parameter which specifies to which folder messages will be saved. It can be both local disk, as well as network location. .PARAMETER FileNameFormat Optional parameter that specifies how individual files will be named based. If omitted, files will be saved in format 'FROM= %SenderName% SUBJECT= %Subject%'. File name can contain any of message parameters surrounded with %. For list of parameters, type Get-OutlookInbox | Get-Member. .OUTPUTS Function is not returning any value. .LINK about_OutlookConnector .NOTES NAME: Export-OutlookMessage AUTHOR: Igor Iric, iric@gmail.com CREATEDATE: September 29, 2015 #> # ---------------------- [Parameters definitions] ------------------------ [CmdletBinding()] Param( [parameter(Mandatory=$true,ValueFromPipeline=$true)][psobject[]]$Messages, [parameter(Mandatory=$true,ValueFromPipeline=$false)][string]$OutputFolder, [parameter(Mandatory=$false,ValueFromPipeline=$false)][string]$FileNameFormat='FROM= %SenderName% SUBJECT= %Subject%' ) #end param # ------------------------- [Function start] ----------------------------- BEGIN { Write-Verbose -Message 'Export-OutlookMessage starting...' $olSaveAsTypes = "Microsoft.Office.Interop.Outlook.olSaveAsType" -as [type] # convert format message to real file name, replace %...% with message attribute $RegEx = '(\%)(.+?)(\%)' } PROCESS { foreach ($Message in $Messages) { # check input object $Props = ($Message | Get-Member).Name if (('Subject' -notin $Props) -or ('SaveAs' -notin $Props)) { Write-Error -Message ('Folder ' + $F.ToString() + ' is not proper object.') Continue # next foreach } Write-Verbose -Message ('Processing '+($Message.Subject)) # creating file name $FileName = $FileNameFormat while ($FileName -match $RegEx) { $property = $Matches[2] if ($Message | Get-Member | Where-Object Name -eq $property) { $FileName = $FileName -replace ('%'+$property+'%'),($Message.($property)) } else { $FileName = $FileName -replace ('%'+$property+'%'),'' Write-Warning -Message ("Message property $property not found. Type `$Message | Get-Member -MemberType Properties to see available properties") } } # fix file name $FileName = Get-ValidFileName -FileName $FileName $FullFilePath = Join-Path -Path $OutputFolder -ChildPath ($FileName + '.msg') # Check if file exists, and if yes, update name with numbering $i = 0 while (Test-Path -Path $FullFilePath) { $i++ $FullFilePath = Join-Path -Path $OutputFolder -ChildPath ($FileName + ' (' + $i + ').msg') } Write-Verbose -Message "Saving message to $FullFilePath" # save message to disk try { $Message.SaveAs($FullFilePath,$olSaveAsTypes::olMSGUnicode) } catch { Write-Error -Message ('Message save exception.'+$Error[0].Exception) } } # end foreach $Message } # End of PROCESS block END { # function closing phase Write-Verbose -Message 'Export-OutlookMessage completed.' } # ------------------------- [End of function] ---------------------------- } |