{"id":305,"date":"2012-07-17T06:55:47","date_gmt":"2012-07-17T11:55:47","guid":{"rendered":"http:\/\/nukeitmike.com\/blog\/?p=305"},"modified":"2023-04-20T20:38:50","modified_gmt":"2023-04-21T01:38:50","slug":"import-msg-files-into-outlook-using-powershell","status":"publish","type":"post","link":"https:\/\/blog.nukeitmike.com\/index.php\/2012\/07\/17\/import-msg-files-into-outlook-using-powershell\/","title":{"rendered":"Import .msg files into Outlook using Powershell"},"content":{"rendered":"<p>We have some old email database backup files that we extracted messages from.&nbsp; 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.&nbsp; 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.<\/p>\n<p>You have to have a machine that has Outlook installed.&nbsp; 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.<\/p>\n<p>First, create the connection to Outlook:<\/p>\n<blockquote><p>$outlook = New-Object -comobject outlook.application<br \/>\n$namespace = $outlook.GetNamespace(&#8220;MAPI&#8221;)<\/p><\/blockquote>\n<p>Then connect to the folder, such as the Inbox:<\/p>\n<blockquote><p>$objInbox&nbsp; = $outlook.Session.GetDefaultFolder(6)<\/p><\/blockquote>\n<p>Other examples:<\/p>\n<blockquote><p>$olAppointmentItem = 1<br \/>\n$olFolderDeletedItems = 3<br \/>\n$olFolderOutbox = 4<br \/>\n$olFolderSentMail = 5<br \/>\n$olFolderInbox = 6<br \/>\n$olFolderCalendar = 9<br \/>\n$olFolderContacts = 10<br \/>\n$olFolderJournal = 11<br \/>\n$olFolderNotes = 12<br \/>\n$olFolderTasks = 13<br \/>\n$olFolderDrafts = 16<\/p>\n<p>$objDraftFolder = $outlook.Session.GetDefaultFolder($olFolderDrafts)<br \/>\n$objDeletedFolder = $outlook.Session.GetDefaultFolder($olFolderDeletedItems)<\/p><\/blockquote>\n<p>I like to know how many messages are in the folder before I begin the import:<\/p>\n<blockquote><p>$colItems = $objDraftFolder.Items&nbsp; #this gets the items in the folder<br \/>\n$FolderItemCount = $colItems.Count #this counts them<br \/>\nWrite-Host $FolderItemCount<\/p><\/blockquote>\n<p>Now you have to open the item and then move it to the folder you want to save it in:<\/p>\n<blockquote><p>$olMailItem = $NameSpace.OpenSharedItem($MailItem)<br \/>\n$olMailItem.Move( $objDraftFolder )&nbsp;&nbsp;&nbsp;<\/p><\/blockquote>\n<p>If you put the above lines in, you will get a lot of data on the screen about the email.&nbsp; To prevent that while still accomplishing the goal of moving the message to Outlook, simply put [void] in front like this:<\/p>\n<blockquote><p>[void]$olMailItem.Move( $objDraftFolder )<\/p><\/blockquote>\n<p>I am working with around a million files, so this was a rather involved script to create.&nbsp; Here is the script I used:<\/p>\n<p>&nbsp;<\/p>\n<blockquote><p>$olMailItemPath = &#8220;F:\\Sorted\\MoveToOutlook\\ByThousands\\*&#8221;<br \/>\n$AfterTime = &#8220;12\/21\/2007&#8221;<br \/>\n$olAppointmentItem = 1<br \/>\n$olFolderDeletedItems = 3<br \/>\n$olFolderOutbox = 4<br \/>\n$olFolderSentMail = 5<br \/>\n$olFolderInbox = 6<br \/>\n$olFolderCalendar = 9<br \/>\n$olFolderContacts = 10<br \/>\n$olFolderJournal = 11<br \/>\n$olFolderNotes = 12<br \/>\n$olFolderTasks = 13<br \/>\n$olFolderDrafts = 16<\/p>\n<p>Write-Host $olMailItemPath<br \/>\n$x=0<br \/>\n$SourceFolders = Get-Item $olMailItemPath<br \/>\necho $SourceFolders.count<br \/>\n$outlook = New-Object -comobject outlook.application<br \/>\n$namespace = $outlook.GetNamespace(&#8220;MAPI&#8221;)<\/p>\n<p>foreach ($_ in $SourceFolders)<br \/>\n&nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp; $SourceFolder = $_<br \/>\n&nbsp;&nbsp;&nbsp; Write-Host &#8220;SourceFolder is $SourceFolder&#8221;<br \/>\n&nbsp;&nbsp;&nbsp; $SourceFiles = Get-ChildItem -path $SourceFolder -recurse -include *.msg&nbsp;&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp; $SFCount = $SourceFiles.count<br \/>\n&nbsp;&nbsp;&nbsp; Write-Host &#8220;Source File Count is $SFCount&#8221;<br \/>\n&nbsp;&nbsp;&nbsp; $objDraftFolder = $outlook.Session.GetDefaultFolder($olFolderDrafts)<br \/>\n&nbsp;&nbsp;&nbsp; $objDeletedFolder = $outlook.Session.GetDefaultFolder($olFolderDeletedItems)<br \/>\n&nbsp;&nbsp;&nbsp; $colItems = $objDraftFolder.Items<br \/>\n&nbsp;&nbsp;&nbsp; $FolderItemCount = $colItems.Count<br \/>\n&nbsp;&nbsp;&nbsp; IF ($FolderItemCount -ge 10000)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Host &#8220;Draft Folder Item Count is $FolderItemCount&#8221;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Host &#8220;Sleeping&#8230;&#8221;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sleep -s 300<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp; foreach ($_ in $SourceFiles)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $x ++<br \/>\n#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Host $x<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $MailItem = $_<br \/>\n#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Host &#8220;Mail Item is $MailItem&#8221;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $olMailItem = $NameSpace.OpenSharedItem($MailItem)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $DateRecieved = $olMailItem.ReceivedTime<br \/>\n#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Host &#8220;Date Recieved is $DateRecieved&#8221;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If ($DateRecieved -le $AfterTime)<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \/>\n#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Host &#8220;Bad Date $DateRecieved&#8221;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [void]$olMailItem.Move( $objDeletedFolder )<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;<br \/>\n#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Host &#8220;Moving $MailItem&#8221;&nbsp;&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [void]$olMailItem.Move( $objDraftFolder )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Write-Host &#8220;Removing $MailItem&#8221;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Remove-Item $MailItem<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>\n&nbsp;&nbsp;&nbsp; }<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>We have some old email database backup files that we extracted messages from.&nbsp; 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.&nbsp; There are better ways to do what we did, than the way we did this,&hellip; <a class=\"more-link\" href=\"https:\/\/blog.nukeitmike.com\/index.php\/2012\/07\/17\/import-msg-files-into-outlook-using-powershell\/\">Continue reading <span class=\"screen-reader-text\">Import .msg files into Outlook using Powershell<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[25],"tags":[],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","category-scripting","entry"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pcW544-4V","_links":{"self":[{"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/posts\/305","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/comments?post=305"}],"version-history":[{"count":2,"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/posts\/305\/revisions"}],"predecessor-version":[{"id":632,"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/posts\/305\/revisions\/632"}],"wp:attachment":[{"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/media?parent=305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/categories?post=305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nukeitmike.com\/index.php\/wp-json\/wp\/v2\/tags?post=305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}