{"id":643,"date":"2015-06-27T20:54:00","date_gmt":"2015-06-27T20:54:00","guid":{"rendered":"https:\/\/www.geekmungus.co.uk\/?p=643"},"modified":"2022-11-05T10:53:19","modified_gmt":"2022-11-05T10:53:19","slug":"microsoft-exchange-2013-automated-pst-import-script","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=643","title":{"rendered":"Microsoft Exchange 2013 Automated PST Import Script"},"content":{"rendered":"\n<p>I needed to have an automated way to bulk import a load of PST files into Exchange 2013. In my case all the files were named as follows:<a href=\"mailto:samAccountName@domain.co.uk\" target=\"_blank\" rel=\"noreferrer noopener\"> <\/a><a href=\"mailto:samAccountName@domain.co.uk\" target=\"_blank\" rel=\"noreferrer noopener\">samAccountName@domain.co.uk<\/a> so for example:<a href=\"mailto:fred.bloggs@thingy.co.uk.pst\" target=\"_blank\" rel=\"noreferrer noopener\"> <\/a><a href=\"mailto:fred.bloggs@thingy.co.uk.pst\" target=\"_blank\" rel=\"noreferrer noopener\">fred.bloggs@thingy.co.uk.pst<\/a><\/p>\n\n\n\n<p>You can run this command from an Exchange PowerShell console, bear in mind that you&#8217;ll need to change your domain name on the New-MailboxImportRequest command as you need to change the FilePath too to match the place were the PST files are for import. Note this must be a UNC path, even if they are stored locally on the Exchange server.<\/p>\n\n\n\n<p>You, if you haven&#8217;t done it already need to grant the user that will be running the task the rights to perform an import or export, to do this run this command, note you only need to do this once.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>New-ManagementRoleAssignment \u2013Role \u201cMailbox Import Export\u201d \u2013User \"DOMAIN\\USER\"<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>New-ManagementRoleAssignment \u2013Role \u201cMailbox Import Export\u201d \u2013SecurityGroup \"DOMAIN\\GROUP\"<\/code><\/pre>\n\n\n\n<p>So here is the script, pop the files in the location, adjust the DOMAIN and filepath to match what you need it to be then you can run it from the Exchange 2013 PowerShell console or from the PowerShell ISE.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>### AutomatedMailboxImport.ps1\n\n### This script automatically imports PST files into a users mailbox based on the name of the file named\n\n# as the username.\n\n# Uncomment this line for testing within the Windows PowerShell ISE.\n\nadd-pssnapin Microsoft.Exchange.Management.PowerShell.E2010\n\n# initialize the items variable with the contents of a directory\n\n$items = Get-ChildItem -Path \"g:\\import\" -Filter *.pst\n\n# Print to the screen, what this script is now doing.\n\nWrite-Host \"Importing PST files into Exchange Mailboxes\"\n\nWrite-Host\n\n# enumerate the items array\n\nforeach ($item in $items)\n\n{\n\n      # if the item is not a directory, then process it.\n\n      if ($item.Attributes -ne \"Directory\")\n\n      {\n\n            Write-Host \"Processing the file: $item\"\n\n            $importfilename = $item.Name\n\n           \n\n            # If it contains a substring of @ then we want to chop this off to create the username.\n\n            if ($item.Name -match '@') {\n\n                # the filename contains a '@' we want to get only the first part of the filename as the username.\n\n                $importusername = ($item.basename -split '@')&#91;0]\n\n            } else {\n\n                # the filename didn't contain a '@', so we can just use the file base name.\n\n                $importusername = $item.BaseName\n\n            }\n\n            Write-Host \"Import Filename: $importfilename\"\n\n            Write-Host \"Username: $importusername\"\n\n            New-MailboxImportRequest -Mailbox \"DOMAIN\\$importusername\" -FilePath \u201c\\\\SERVER\\g$\\import\\$importfilename\" -BadItemLimit Unlimited \u2013AcceptLargeDataLoss\n\n      }\n\n}<\/code><\/pre>\n\n\n\n<p>Once you&#8217;ve run it, it will have queued up all the imports, you can look at their current state of import with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>get-mailboximportrequest | get-mailboximportrequeststatistics<\/code><\/pre>\n\n\n\n<p>Once they have all been imported, you can clean them up with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Get-MailboxImportRequest -Status Completed | Remove-MailboxImportRequest<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I needed to have an automated way to bulk import a load of PST files into Exchange 2013. In my case all the files were named as follows: samAccountName@domain.co.uk so for example: fred.bloggs@thingy.co.uk.pst You can run this command from an Exchange PowerShell console, bear in mind that you&#8217;ll need to change your domain name on &#8230; <a title=\"Microsoft Exchange 2013 Automated PST Import Script\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=643\" aria-label=\"Read more about Microsoft Exchange 2013 Automated PST Import Script\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32],"tags":[],"class_list":["post-643","post","type-post","status-publish","format-standard","hentry","category-microsoft-exchange"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=643"}],"version-history":[{"count":1,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/643\/revisions"}],"predecessor-version":[{"id":1431,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/643\/revisions\/1431"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}