Exporting Mails from Microsoft Outlook to CSV file with Powershell

A small Powershell script I created for exporting emails from MS Outlook to CSV file. It retrieves Date, Time, Subject, To, From, CC, BCC and Body fields. The reason i wrote this script is an Outlook’s Export to CSV function failure to perform export properly. It put internal X500 type of address (LegacyExchangeDN attribute) for non SMTP emails. My script doesn’t have this problem and resolves all X500 address to SMTP standard.

It uses Outlook API (Office Outlook Primary Interop Assembly) and requires MS Outlook to be installed on a system where you run the script.

Script has 4 variables defined at the beggining of a code. You may need to change some values to adopt script to your system.

$maxRecordToProcessForEachFolder – limit of records to process for each folder, Low values are good for trial runs.

$rootFolderName – Outlook’s root folder name. You can get this name from first run of a program – it will print root folders connected to Outlook. This also can be connected PST file name if you have any.

$pathToInteropDLL – in your computer you might need to locate Microsoft.Office.Interop.Outlook.dll and place path to it in this variable. $foldersToExport – set of folders (under the root folder) to perform export from. Will also include subfolders if they have any.

I also have next version which utilizes MS Graph API and reads mails directrly from Exchange online. It doesn’t require Outlook to be installed and works several times faster.

Leave a Reply

Your email address will not be published. Required fields are marked *