Script Request: Create Outlook PST File

Greetings,

Would it be possible to create a script that would export the current user’s Outlook mailbox to a local PST file?

Thanks!
Matt

Hi @ntelogic,

We have asked our script developers to check and provide feedback.

Kind Regards,
PremJK

Hi @ntelogic,

We are extremely sorry to inform you that this script is not feasible ,since we don’t have any command line switch for outlook to export outlook profile as pst.
This script request is not doable.

Kind Regards,
PremJK

@ntelogic

Scripting export of email to PST from just local Outlook profiles is tricky outside of using a paid third-party tool. If needing to export from a mailserver like exchange or Office 365, exporting to PST can be done fairly easily with powershell.

I dont know if this is helpful to you or not, but I have some scripts that will export current user outlook email folders to disk (or network location) as .msg files. Working with .msg files is not nearly as inclusive as PST, but can be an option if you only need email messages backed up. If you think this would be helpful, let me know and I can share this script.

I’ve looked into trying to improve this script to get the export to go to PST, but so far have had no luck. I can do almost everything else, including attach and import emails FROM a PST, but unable to go the other direction.

@eztech Thank you for your kind offer! Yes, we’d appreciate your sharing of the script.

@ntelogic
Roger that. Give me about a day or so, as I need to clean up the script code, make some adjustments and put into a ITSM procedure you can import easily. I’ll post back here soon!

@eztech Many thanks!

@ntelogic
Below is the script along with some documentation/instructions on how to use it and what you can configure. Hopefully its clear and understandable. The code itself is a bit messy and there is a bunch of unused/test code in there as this is something I have been testing and cobbling together, however feel free to change anything you see fit.

If you have any questions, issues or suggestions feel free to let me know. Will share anything here in case anyone else finds it useful.

Thanks!

Export-OutlookMessages -
Version: 0.2

SYNOPSIS
Powershell Script that connects to logged-in users outlook context to retrieve and optionally export outlook profiles and emails. This script is executed from a python script within an ITSM procedure.

Requirements

  • Powershell v3.0 or higher
  • Windows 10. “Should” work on other versions as long as powershell v3 or higher is installed, however no other OS’s have been tested.
  • Tested on latest version of Outlook provided by Office 365. “Should” work with Outlook versions 2019/2016 and perhaps lower. Sometimes third-party outlook plugins can cause issues, disable those first if having issues.
  • Must be run under Current Logged-In User context.

Full Description
This script provides the ability to export emails from Outlook to local disk or network location in the .MSG format. It must be run under the current user context and will connect to Outlooks default profile.
If you wish to only retrieve a list of any and all Outlook profiles or mailboxes connected, you can enable listonly mode in the configuration variables. Doing so will not perform any export.
This script can be used as a method to automatically backup emails on a local machine. It is not intended to replace email backup solutions, but can be used when backup options are limited or not available.
Another potential use case is to backup emails from an Outlook client that is no longer connected to the mail server and thus emails cannot be saved server side. An example of this is if there is an exchange crash/loss and the only copy of emails for an account are within the cached OST of an Outlook client. That use case was the main factor in the decision to create this script.

Exported emails will be put into a folder structure as follows:

  • ExportFolderPath + \OutlookEmailAccount + \EmailFolderName +\FROM=*Subject=*.msg
  • Example: C:\emailexport estuser@example.com\Inbox\FROM=FromEmailAccount SUBJECT=TestSubject.msg
  • A separate folder will be created for each email folder being exported
Configurable Variables These are configuration variables within the script along with a description of what they do and example value. Within ITSM, these variables are configured using ITSM parameters to avoid the need to edit the code directly
  • mainaccountonly- True | False - Set True to only retrieve email folders and messages from the main account in the current outlook profile. Setting to false will retrieve email folders and messages from all accounts/mailboxes connected to the current outlook profile
    • Example/Default Value: True
  • listonly- True | False - Set to True to only output a list of email accounts and folders connected within the current Outlook profile without doing any export. Set to False to enable exporting of emails
    • Example/Default Value: False
  • exportfolder- UNC path where emails will be exported as .msg files. It can be both local disk, as well as network location. If folder does not exist in the specified location, it will be created. Entire Outlok folder structure will be generated within specified folder.
    • Example/Default Value: C:\emailexport\
  • emailfolders- List the names of each of the email folders you wish to export emails from. At least one folder must be specified in order to export emails. NOTE: Each folder name in the list must be within quotes and comma separated. See example
    • Example/Default Value: "Inbox","Sent"
  • openoutlook- True | False - Set True to automatically launch Outlook if currently not running before executing any further script actions. This is only recommended if you are receiving error "System.Runtime.InteropServices.COMException (0x80080005)".
    • Example/Default Value: False
Credits Write-Color - https://github.com/EvotecIT/PSWriteColor OutlookConnector - https://www.powershellgallery.com/packages/OutlookConnector/

NOTES
Author: EZTechhelp

IMPORTANT
This script was tested using an Outlook client connected to an Office 365 email account. While it should work for any kind of email account connected to outlook, results may vary. It is also important to understand that execution of the script may take a long time depending on the size/number of email being exported. If an error occurs on exporting a specific message (due to corruption or other factors) the script will skip the message and continue to the next one, unless an unrecoverable error occurs which will cause the script to abort. The script only reads from Outlook and performs no write attempts other than to the specified path for exportfolder. Therefore, the script should not cause any issues with or disrupt data in outlook itself.

How to Install

  • Download the ITSM procedure:
  • Within your ITSM portal, import the procedure under Configuration Templates - Procedures
  • Under the tab Parameters, configure each parameter value to fit your needs. See Configurable Variables for explanation and examples.
  • Click Save - Ready to Review - Approve to finish
  • (Recommended) Run the new procedure (make sure to choose run as logged-in user) on a single test machine, preferrable with listonly set to $true first to see if the script is able to successfully connect to the outlook profile
Example Output with listonly ```
    ##### Getting Outlook Profiles #####

    Connecting to Outlook Session

    Current Profile Name : Outlook
    Current User : Test User
    Connection Type : Mapi
    Exchange Connection Mode : olCachedDisconnected
    Exchange Mailbox Server Name : https://outlook.office365.com/mapi/e…=xxxxx@example.com
    Exchange Mailbox Server Version: 15.20.3890.28

    Getting All Outlook Accounts/Folders in Current Profile

    Connecting to Outlook Session

    Account Name : testuser@example.com
    File Path : C:\Users estuser\AppData\Local\Microsoft\Outlook\ testuser@example.com.ost
    IsOpen : True
    IsCachedExchange: True
    IsDataFileStore : True

    Account Name : TestSharedMailAccount
    File Path :
    IsOpen : True
    IsCachedExchange: False
    IsDataFileStore : False

    Getting Outlook Folder Inbox From Main Account in Profile

    Connecting to Outlook Session

    Getting Outlook Folder Sent From Main Account in Profile

    Connecting to Outlook Session

    Name FullFolderPath UnReadItemCount


    Inbox \ estuser@example.com\Inbox 918
    Sent \ estuser@example.com\Inbox\Sent 0




<b>Example Output with Export</b>
    ##### Getting Outlook Profiles #####

    Connecting to Outlook Session

    Current Profile Name : Outlook
    Current User : Test User
    Connection Type : Mapi
    Exchange Connection Mode : olCachedConnectedFull
    Exchange Mailbox Server Name : https://outlook.office365.com/mapi/e…=xxxxx@example.com
    Exchange Mailbox Server Version: 15.20.3890.28

    Getting All Outlook Accounts/Folders in Current Profile

    Connecting to Outlook Session
    Added 2 base folders

    Get-OutlookFolder finished

    Account Name : testuser@example.com
    File Path : C:\Users estuser\AppData\Local\Microsoft\Outlook\ testuser@example.com.ost
    IsOpen : True
    IsCachedExchange: True
    IsDataFileStore : True

    Account Name : TestSharedMailbox
    File Path :
    IsOpen : True
    IsCachedExchange: False
    IsDataFileStore : False

    Getting Outlook Folder ‘Inbox’ From Main Account in Profile

    Connecting to Outlook Session
    Added 2 base folders
    Filtering out folders not in main mailbox.
    Remaining 1 base folders.

    Get-OutlookFolder finished

    Exporting Outlook Emails from folder ‘Inbox’

    Connecting to Outlook Session
    Export-OutlookFolder starting…
    Added 2 base folders
    Filtering out folders not in main mailbox.
    Remaining 1 base folders.
    …Checking: \ estuser@example.com\Inbox
    …Exporting \ estuser@example.com\Inbox , 19 message(s)

    Export-OutlookMessage starting…
    …Processing: Test Email 1
    …Saving message to C:\emailexport estuser@example.com\Inbox\FROM= Support SUBJECT= Test Email 1.msg

    Export-OutlookMessage completed.
    Export-OutlookMessage starting…
    …Processing: Test Email 2
    …Saving message to C:\emailexport estuser@example.com\Inbox\FROM= Support SUBJECT= Test Email 2.msg

    Export-OutlookMessage completed.
    Export-OutlookMessage starting…
    …Processing: Test Email 3
    …Saving message to C:\emailexport estuser@example.com\Inbox\FROM= Support SUBJECT= Test Email 3.msg

    Export-OutlookMessage completed.
    Export-OutlookMessage starting…
    …Processing: Test Email 4
    …Saving message to C:\emailexport estuser@example.com\Inbox\FROM= Support SUBJECT= Test Email 4.msg

    Export-OutlookMessage completed.
    Export-OutlookMessage starting…
    …Processing: Test Email 5
    …Saving message to C:\emailexport estuser@example.com\Inbox\FROM= Support SUBJECT= Test Email 5.msg

    Export-OutlookMessage completed.
    Get-OutlookFolder finished
    Export-OutlookFolder completed.


[20210303-Export-OutlookMessages.json|attachment](upload://7X01SG78Gd9BNiHhApywNhckofO.json) (67.4 KB)

@eztech many thanks for the script! We’ll give a run and let you know how it goes.