Saving e-mail from Exchange Online to the filesystem using Microsoft Graph

At a recent work related conference, where for a change of pace I was helping out on a IT Helpdesk, I was asked by someone if there was an easy way to save emails from Outlook to their OneDrive account.

While this is a relatively easy task for small numbers of emails where you can drag and drop them to a synced OneDrive folder on the local desktop, in this case they had a full folder structure of emails which had grown out of necessity and time restrictions and it isn’t possible to drag and drop folders in the same way.

Now, the obvious thought was to use the export function from Outlook, but this results in a PST-based mail archive and this wasn’t what the requestor wanted.

Now, over the last year I have been increasingly using the MSGraph PowerShell module to rewrite a lot of my scripts which had been using now deprecated libraries, so I thought this would be worth a go.

Unsurprisingly, MS Graph again came to the rescue and I thought (despite this being a bit of a niche requirment) I would share the resulting script at https://github.com/imnota/export-emailsToFiles

To use the script, you need delegated permissions on the target mailbox and Mail.Read.Shared permissions on Graph.

The script will find the first occurrence of the folder name you specify and recursively save folders of emails under that folder (duplicate starting folder names will be looked into when I have a little more time).

It’s relatively simple to use, as can be seen in this example:

.\Export-emailsToFiles.ps1 -userPrincipalName joe.bloggs@example.domain.com -emailFolderName "Inbox" -fileFolderRoot C:\EmailExtract

Of course, if you have a synced OneDrive folder you want to extract to, you can use that in the “fileFolderRoot” parameter.

While the script may not be perfect, hopefully it will prove useful to someone out there!

Leave a Reply

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