Import .msg files into Outlook using Powershell

We have some old email database backup files that we extracted messages from.  The purpose of this was to be able to expire the backups and do away with them, while keeping the messages in our Journal for e-discovery purposes.  There are better ways to do what we did, than the way we did this, but it has been a process of learning, and one of the things I was able to learn is how to import .msg files into Outlook.

You have to have a machine that has Outlook installed.  Outlook 2007 is the version I used. This would work with Outlook 2010, but you will get a popup about allowing scripting access to Outlook.

First, create the connection to Outlook:

$outlook = New-Object -comobject outlook.application
$namespace = $outlook.GetNamespace(“MAPI”)

Then connect to the folder, such as the Inbox:

$objInbox  = $outlook.Session.GetDefaultFolder(6)

Other examples:

$olAppointmentItem = 1
$olFolderDeletedItems = 3
$olFolderOutbox = 4
$olFolderSentMail = 5
$olFolderInbox = 6
$olFolderCalendar = 9
$olFolderContacts = 10
$olFolderJournal = 11
$olFolderNotes = 12
$olFolderTasks = 13
$olFolderDrafts = 16

$objDraftFolder = $outlook.Session.GetDefaultFolder($olFolderDrafts)
$objDeletedFolder = $outlook.Session.GetDefaultFolder($olFolderDeletedItems)

I like to know how many messages are in the folder before I begin the import:

$colItems = $objDraftFolder.Items  #this gets the items in the folder
$FolderItemCount = $colItems.Count #this counts them
Write-Host $FolderItemCount

Now you have to open the item and then move it to the folder you want to save it in:

$olMailItem = $NameSpace.OpenSharedItem($MailItem)
$olMailItem.Move( $objDraftFolder )   

If you put the above lines in, you will get a lot of data on the screen about the email.  To prevent that while still accomplishing the goal of moving the message to Outlook, simply put [void] in front like this:

[void]$olMailItem.Move( $objDraftFolder )

I am working with around a million files, so this was a rather involved script to create.  Here is the script I used:

 

$olMailItemPath = “F:\Sorted\MoveToOutlook\ByThousands\*”
$AfterTime = “12/21/2007”
$olAppointmentItem = 1
$olFolderDeletedItems = 3
$olFolderOutbox = 4
$olFolderSentMail = 5
$olFolderInbox = 6
$olFolderCalendar = 9
$olFolderContacts = 10
$olFolderJournal = 11
$olFolderNotes = 12
$olFolderTasks = 13
$olFolderDrafts = 16

Write-Host $olMailItemPath
$x=0
$SourceFolders = Get-Item $olMailItemPath
echo $SourceFolders.count
$outlook = New-Object -comobject outlook.application
$namespace = $outlook.GetNamespace(“MAPI”)

foreach ($_ in $SourceFolders)
    {
    $SourceFolder = $_
    Write-Host “SourceFolder is $SourceFolder”
    $SourceFiles = Get-ChildItem -path $SourceFolder -recurse -include *.msg   
    $SFCount = $SourceFiles.count
    Write-Host “Source File Count is $SFCount”
    $objDraftFolder = $outlook.Session.GetDefaultFolder($olFolderDrafts)
    $objDeletedFolder = $outlook.Session.GetDefaultFolder($olFolderDeletedItems)
    $colItems = $objDraftFolder.Items
    $FolderItemCount = $colItems.Count
    IF ($FolderItemCount -ge 10000)
        {
            Write-Host “Draft Folder Item Count is $FolderItemCount”
            Write-Host “Sleeping…”
            sleep -s 300
        }
    foreach ($_ in $SourceFiles)
        {
        $x ++
#         Write-Host $x
        $MailItem = $_
#         Write-Host “Mail Item is $MailItem”
        $olMailItem = $NameSpace.OpenSharedItem($MailItem)
        $DateRecieved = $olMailItem.ReceivedTime
#         Write-Host “Date Recieved is $DateRecieved”
        If ($DateRecieved -le $AfterTime)
            {
#             Write-Host “Bad Date $DateRecieved”
            [void]$olMailItem.Move( $objDeletedFolder )
            }
        else
            {   
#             Write-Host “Moving $MailItem”   
            [void]$olMailItem.Move( $objDraftFolder )       
            }
#         Write-Host “Removing $MailItem”
        Remove-Item $MailItem
        }
    }

7 comments

  1. thanks
    it was a huge help
    I use win 10 technical preview
    with office 2013
    i had to add
    $olMailItem = $NameSpace.OpenSharedItem($MailItem.FullName)
    instead of
    $olMailItem = $NameSpace.OpenSharedItem($MailItem)

    it may help someone

    thanks again

  2. Thank you for writing this code down. It’s really helpful.
    However, I am trying to do editing of .msg file that resides in local drive.
    I like to edit following i.e. Subject (only wants to replace certain word not entire subject). Once completed then I want to save the .msg file to new directory or folder.

    I am having difficulty in saving the file as .msg. would it be possible if this could be achieved with Outlook ?

  3. Umair, I never had to change the subject. I was basically just importing .msg files that had been extracted with another tool. I suspect, you could change the subject, but then you would be creating a new message file.

Leave a Reply