Using PowerShell to attach PST files to Outlook


This one is a really quick note. Yet another example to show off the power of the PowerShell pipeline and its interoperability with the COM world.

Recently I had to add 30+ existing PST files to an instance of Outlook. I hate using the mouse and keyboard for repetitive tasks (File –> Open Outlook Data File –> browse to the path –> click OK), so I set out to do the same through some form of scripting.

Luckily, PowerShell can create instances of COM objects, and Outlook provides a COM library to automate Outlook. It was a matter of a few minutes then to look around on Bing and compose the script:

Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
dir “c:\mypath\*.pst” | % { $namespace.AddStore($_.FullName) }

One interesting thing with this is that you may need to run this with the correct version of PowerShell (32-bit is at c:\windows\syswow64\windowspowershell\v1.0\powershell.exe) to match the bitness of Outlook installed on your machine.

Also, if you do plan to use the above script, make sure you replace the path with the correct ones. Also do note that if the PST file path is incorrectly specified, Outlook will create a PST at that location, so be careful!

The AddStore method is documented here. Do note though this COM interface does not permit the addition of password protected PST files. In that case you may want to explore the third-party Redemption library.

Disclaimer

Note: Please note third-party links are provided as-is and Microsoft does not offer any guarantees or warranties regarding the content on the third party site.

This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment.  THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.  We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that You agree: (i) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded; (ii) to include a valid copyright notice on Your software product in which the Sample Code is embedded; and (iii) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of the Sample Code.

This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm.

Comments (3)

  1. Kamat says:

    Hi Arvind,

    I need to export a specific folder within a PST (imported using your method) to CSV

    There are currently 30 PST's and doing it manually is time consuming. Can you provide a script which helps me to do

  2. Anon says:

    Here's a version which works with Redemption to add password protected PSTs:

    Add-type -path "c:\redemption\Interop.Redemption.dll"
    $cred = Get-Credential
    $rdosession = new-object -comobject Redemption.RDOSession
    $rdosession.Logon()
    dir C:\mailbox\*.pst | % { $rdosession.Stores.AddPstStoreWithPassword(($_.FullName), $null, ($_.Name), $cred.GetNetworkCredential().Password) }

  3. Quite impressive that a Shell script can import PST file to Outlook.
    Everyone is not that good at shell script though given an example with. Trying script is an option but during the process, one might lose their important PST files.
    For avoiding such accidents use Add Outlook PST Tools to Import multiple PST file into Outlook at one time, without losing any data.
    Visit: http://www.addoutlookpst.com/

Skip to main content