Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions Export-OutlookFolder.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ Get-OutlookFolder -Recurse -MainOnly | ? Name -eq 'Done' | Export-OutlookFolder
Saves all messages from folder named 'Done' to a disk using default file naming.

.EXAMPLE
Get-OutlookFolder -MainOnly | Export-OutlookFolder -OutputFolder 'C:\tmp\all' -Progress -WarningAction SilentlyContinue
Saves all messages from main mailbox 'C:\tmp\all' to a disk using default file naming. Warnings for messages without subject used in file naming, are ignored.
Get-OutlookFolder -recurse | WHERE Name -in 'Home','Finance' | Export-OutlookFolder -OutputFolder 'C:\temp\Export-Mail-Temp' -ExportFormat HTML
Saves all messages from folders and sub-folders of 'Home' and 'Finance' (if not empty) to folder 'C:\temp\Export-Mail-Temp' in the HTML format.
Using this feature, one can avoid passing objects from Get-OutlookFolder via Export-OutlookMessage over to Export-OutlookMessageBody,
in order to export folder messages to HTML format.

.PARAMETER InputFolder
Mandatory parameter that specifies which Outlook folder needs to be exported. Easies is to obtain it via
Expand All @@ -31,6 +33,11 @@ File name can contain any of message parameters surrounded with %. For list of p
Custom format can be specified after a | character within the %, e.g. %ReceivedTime|yyyyMMddhhmmss%.
Parameter is passed to Export-OutlookMessage function.

.PARAMETER ExportFormat
Optional parameter which specifies to which format message body will be exported to. Allowed values are MSG (default), HTML, TXT (text) and RTF (rich-text).
In case of MSG, the Export-OutlookMessage will be called to do the job. For all others, the Export-OutlookMessageBody will be called.
This makes it possible to for example export folders from Outlook to HTML-format

.PARAMETER Filter
Optional parameter that can contain a filter string expression to be applied to restrict items to be exported.
For syntax see https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/items-restrict-method-outlook.
Expand Down Expand Up @@ -58,6 +65,7 @@ about_OutlookConnector
NAME: Export-OutlookFolder
AUTHOR: Igor Iric, iricigor@gmail.com
CREATEDATE: September 29, 2015
UPDATE: May 18, 2020 by Peter Rosenberg aka PetRose
#>

# ---------------------- [Parameters definitions] ------------------------
Expand All @@ -68,6 +76,7 @@ Param(
[parameter(Mandatory=$true,ValueFromPipeline=$true,Position=0)][psobject[]]$InputFolder,
[parameter(Mandatory=$true,ValueFromPipeline=$false)][string]$OutputFolder,
[parameter(Mandatory=$false,ValueFromPipeline=$false)][string]$FileNameFormat='FROM= %SenderName% SUBJECT= %Subject%',
[parameter(Mandatory=$false,ValueFromPipeline=$false)][string][ValidateSet('MSG','HTML','TXT','RTF')] [string]$ExportFormat='MSG',
[parameter(Mandatory=$false,ValueFromPipeline=$false)][string]$Filter,
[parameter(Mandatory=$false,ValueFromPipeline=$false)][Microsoft.Office.Interop.Outlook.OlObjectClass[]]$IncludeTypes,
[parameter(Mandatory=$false,ValueFromPipeline=$false)][Microsoft.Office.Interop.Outlook.OlObjectClass[]]$ExcludeTypes,
Expand Down Expand Up @@ -128,7 +137,9 @@ PROCESS {
Continue folderloop # next folder
}
}
Export-OutlookMessage -Messages $msg -OutputFolder $TargetFolder -FileNameFormat $FileNameFormat
if ($Exportformat -eq 'MSG') {
Export-OutlookMessage -Messages $msg -OutputFolder $TargetFolder -FileNameFormat $FileNameFormat
} else { Export-OutlookMessageBody -Messages $msg -OutputFolder $TargetFolder -FileNameFormat $FileNameFormat -ExportFormat $ExportFormat}
++$exportCounter
} else {
Write-Verbose -Message ('Excluding message of type ' + [enum]::GetName([Microsoft.Office.Interop.Outlook.OlObjectClass], $msg.Class))
Expand All @@ -141,7 +152,7 @@ PROCESS {
if ($SubCount -gt 0) {
# export subfolders
foreach ($subfolder in ($F.Folders)) {
Export-OutlookFolder -InputFolder $subfolder -OutputFolder $OutputFolderPath -FileNameFormat $FileNameFormat -Filter $Filter -IncludeTypes $IncludeTypes -ExcludeTypes $ExcludeTypes -Progress:$Progress
Export-OutlookFolder -InputFolder $subfolder -OutputFolder $OutputFolderPath -FileNameFormat $FileNameFormat -ExportFormat $ExportFormat -Filter $Filter -IncludeTypes $IncludeTypes -ExcludeTypes $ExcludeTypes -Progress:$Progress
}
}
} # End of foreach
Expand Down
Binary file modified OutlookConnector.psd1
Binary file not shown.
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Get functions are returning all properties of a message, so it's good practice t

### Saving messages to disk
- - - - - - - - - - - -
- **Export-OutlookFolder** –InputFolder -OutputFolder -FilenameFormat - saves all messages to folder on a disk
- **Export-OutlookFolder** –InputFolder -OutputFolder -FilenameFormat [-ExportFormat] - saves all messages to folder on a disk, in one of 4 formats MSG, HTML, RTF or TXT
- **Export-OutlookMessage** –Message –OutputFolder -FileNameFormat - saves individual message to folder on a disk
Input parameter (folder or message) can be piped. Export functions are saving messages in individual MSG files.

Expand All @@ -53,6 +53,7 @@ Input parameter (folder or message) can be piped. Export functions are saving me
- - - -
```powershell
Get-OutlookFolder | Export-OutlookFolder 'C:\Email' # saves all emails to disk
Get-OutlookFolder -recurse | WHERE Name -in 'Home','Finance' | Export-OutlookFolder -OutputFolder 'C:\Export-Mail' -ExportFormat HTML # saves messages from two folders to disk in HTML format
Get-OutlookInbox -Verbose | Group Sendername # group inbox messages by sendername
Get-Help Export-OutlookMessage -Examples # all messages have standard synopsis
```
Expand All @@ -62,10 +63,24 @@ Get-Help Export-OutlookMessage -Examples # all messages have standa
- **0.90** - Sep '15 - initial version, 7 functions, read only access to data
- **0.91** - Oct '15 - split to multiple files, separate module and manifest file, more help and verbose
- **0.92** - Nov '15 - 2nd public release, one more command added (Export Body), corrected email address
- **0.93** - May '20 - Added feature to allow Export-OutlookFolder to accept ExportFormat parameter in order to handle all formats


## Requests for next versions
- - - - - - - - - - - - -
- Export-OutlookFolder - add failed messages to error variable; this functionality is implemented in other Export-Outlook* functions

***Any further suggestion is welcome!***

**PetRose**: Found that it was difficult perhaps even impossible, to pass Outvariable from one of the functions to the other using pipes.
I needed this 'logically equivalent' capability of this:
Get-OutlookFolder -recurse -Progress | ? Name -eq 'Home' | Export-OutlookMessageBody -OutputFolder 'C:\temp\Export-Mail' -ExportFormat HTML
but this throws error:
Export-OutlookMessageBody : Message of type Folder is not proper object. Missing: Subject, HTMLBody, RTFBody, Body, SenderName, Subject.

So, I took the opportunity to enhance the Export-OutlookFolder function to accept the -ExportFormat parameter and then simply pass its value to either:
Export-OutlookMessage (in the case of MSG format)
or
Export-OutlookMessageBody (in all other cases)

Its tested, and seems to work as version 0.93