'PowerShell - Automate loading .pst files into Outlook and exporting individual .msg items inside
I am currently working on a project where I have been given a lot of .pst files. These files contain individual .msg files inside which I need to run an analysis on. I am planning to read the .msg files with Python and run my analysis there, but right now I am struggling with working with the .pst files.
I need to automate the process of loading the .pst files and exporting the individual .msg files inside. It seems PowerShell is the best solution for this given it's integration with Outlook but I am struggling to find any solutions online on how I can achieve this. Does anyone have any solutions for this problem?
Solution 1:[1]
The PST file format is described in the Outlook Personal Folders (.pst) File Format section of MSDN.
It is not clear when and where you are going to run the code... Remember that Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.
So, if you are going to run the code on the server-side or from a windows service, Outlook automation is not for you. You need to consider any third-party libraries or components designed for server-side execution.
For example, you may take a look at pypff which is a python wrapper for the C library libpff
that allows accessing emails and the directory structure of Pst files within python.
import pypff
pst = pypff.file()
pst.open("MyPst.pst")
pst.close()
Also, you may find the Read PST files from win32 or pypff page helpful.
Solution 2:[2]
You can create an instance of the Outlook.Application COM object and use its Namespace.AddStore/AddStoreEx method to open a PST file (you would think the method woudl return the newly added store, but it does not). You can then find that store in the Namespace.Stores collection and process its folders and messages, calling MailItem.SaveAs(.., olMsg) for each message.
If using Redemption (I am its author) is an option, it will let you open PST file without polluting the primary profile with the PST files - call RDOSession.LogonPstStore
(it creates and deletes a temporary profile configured to use the specified PST file and returns the RDOPstStore object). Start with RDOStore.RootIPMFolder
and process all the subfolders (RDOFolder.Folders
) and messages (RDOFolder.Items
) calling RDOMail.SaveAs(..., olMsg)
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Eugene Astafiev |
Solution 2 |