{"id":534,"date":"2026-03-17T21:46:27","date_gmt":"2026-03-17T21:46:27","guid":{"rendered":"https:\/\/xuan.co.uk\/?p=534"},"modified":"2026-03-17T21:46:27","modified_gmt":"2026-03-17T21:46:27","slug":"powershell-to-move-outlook-messages-from-one-folder-to-another","status":"publish","type":"post","link":"https:\/\/xuan.co.uk\/?p=534","title":{"rendered":"Powershell to move Outlook messages from one folder to another"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n# 1. INITIALISATION &amp; INSTRUCTIONS<br \/>\n# &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\nWrite-Host &#8220;================================================================&#8221;<br \/>\nWrite-Host &#8220;.SYNOPSIS&#8221;<br \/>\nWrite-Host &#8220;Moves a specified number of emails from a source folder to a&#8221;<br \/>\nWrite-Host &#8220;destination folder in Outlook.&#8221;<br \/>\nWrite-Host &#8220;&#8221;<br \/>\nWrite-Host &#8220;.DESCRIPTION&#8221;<br \/>\nWrite-Host &#8220;Usage Instructions:&#8221;<br \/>\nWrite-Host &#8220;1. Ensure Outlook is running with the same privileges as PowerShell.&#8221;<br \/>\nWrite-Host &#8220;2. Select the SOURCE folder when prompted.&#8221;<br \/>\nWrite-Host &#8220;3. Select the DESTINATION folder when prompted.&#8221;<br \/>\nWrite-Host &#8220;4. Enter the specific number of emails to move (Enter 0 for ALL).&#8221;<br \/>\nWrite-Host &#8220;================================================================`n&#8221;<\/p>\n<p># Create a COM object to interact with the Outlook application.<br \/>\n$outlook = New-Object -ComObject Outlook.Application<br \/>\n# Access the Messaging Application Programming Interface (MAPI) namespace,<br \/>\n# which is required to access Outlook folders and items.<br \/>\n$namespace = $outlook.GetNameSpace(&#8220;MAPI&#8221;)<\/p>\n<p># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n# 2. FOLDER SELECTION<br \/>\n# &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n# Prompt the user with a dialogue box to select the source folder.<br \/>\nWrite-Host &#8220;Select the SOURCE folder&#8230;&#8221;<br \/>\n$source = $namespace.PickFolder()<br \/>\n# Exit the script if the user clicks &#8216;Cancel&#8217; or closes the prompt.<br \/>\nif ($null -eq $source) { exit }<\/p>\n<p># Prompt the user with a dialogue box to select the destination folder.<br \/>\nWrite-Host &#8220;Select the DESTINATION folder&#8230;&#8221;<br \/>\n$destination = $namespace.PickFolder()<br \/>\n# Exit the script if no destination is selected, or if the source and<br \/>\n# destination folders are exactly the same (preventing an infinite loop).<br \/>\nif ($null -eq $destination -or $source.EntryID -eq $destination.EntryID) { exit }<\/p>\n<p># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n# 3. ITEM INVENTORY<br \/>\n# &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n# Retrieve all items (emails, meetings, etc.) within the selected source folder.<br \/>\n$items = $source.Items<br \/>\n# Count the total number of items present.<br \/>\n$itemCount = $items.Count<\/p>\n<p>Write-Host &#8220;`nFound $itemCount item(s) in $($source.Name).&#8221;<\/p>\n<p># If the folder is empty, terminate the script as there is nothing to do.<br \/>\nif ($itemCount -eq 0) {<br \/>\nWrite-Host &#8220;No items to move.&#8221;<br \/>\nexit<br \/>\n}<\/p>\n<p># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n# 4. USER INPUT &amp; VALIDATION<br \/>\n# &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n# Initialise the limit variable.<br \/>\n[int]$limit = -1<\/p>\n<p># Create an infinite loop that only breaks when valid input is provided.<br \/>\nwhile ($true) {<br \/>\n$userInput = Read-Host &#8220;How many emails would you like to move? (Enter 0 for ALL)&#8221;<br \/>\n# Attempt to parse the input as an integer. It must be a number and 0 or greater.<br \/>\nif ([int]::TryParse($userInput, [ref]$limit) -and $limit -ge 0) {<br \/>\nbreak # Exit the loop if valid.<br \/>\n}<br \/>\nWrite-Host &#8220;Please enter a valid number (0 or greater).&#8221;<br \/>\n}<\/p>\n<p># If the user enters 0 (meaning &#8216;all&#8217;), or asks to move more items than<br \/>\n# actually exist, cap the limit at the total number of available items.<br \/>\nif ($limit -eq 0 -or $limit -gt $itemCount) {<br \/>\n$limit = $itemCount<br \/>\nWrite-Host &#8220;Setting limit to $itemCount (all available items).&#8221;<br \/>\n}<\/p>\n<p># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n# 5. EXECUTION &amp; MOVEMENT<br \/>\n# &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br \/>\n# Calculate the index at which the loop should stop to move exactly<br \/>\n# the requested amount of items.<br \/>\n$stopAt = $itemCount &#8211; $limit + 1<br \/>\n$processed = 0<\/p>\n<p># Loop through the items BACKWARDS. This is critical when modifying COM collections;<br \/>\n# moving item #1 shifts item #2 to position #1, breaking a standard forward loop.<br \/>\nfor ($i = $itemCount; $i -ge $stopAt; $i&#8211;) {<br \/>\n$processed++<\/p>\n<p># Update the visual progress bar in the PowerShell console.<br \/>\nWrite-Progress -Activity &#8220;Moving Emails&#8221; -Status &#8220;Processed $processed of $limit&#8221; -PercentComplete (($processed \/ $limit) * 100)<\/p>\n<p># Execute the move command. Casting to [void] prevents the COM object<br \/>\n# from outputting enumeration data to the pipeline, which can cause failures.<br \/>\n[void]$items.Item($i).Move($destination)<br \/>\n}<\/p>\n<p># Clear the progress bar from the screen once finished.<br \/>\nWrite-Progress -Activity &#8220;Moving Emails&#8221; -Completed<\/p>\n<p># Display the final summary.<br \/>\nWrite-Host &#8220;`nCompleted. Moved $limit email(s).&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; # &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- # 1. INITIALISATION &amp; INSTRUCTIONS # &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- Write-Host &#8220;================================================================&#8221; Write-Host &#8220;.SYNOPSIS&#8221; Write-Host &#8220;Moves a specified number of emails from a source folder to a&#8221; Write-Host &#8220;destination folder in Outlook.&#8221; Write-Host &#8220;&#8221; Write-Host &#8220;.DESCRIPTION&#8221; Write-Host &#8220;Usage Instructions:&#8221; Write-Host &#8220;1. Ensure Outlook is running with the same privileges as <a class=\"more-link\" href=\"https:\/\/xuan.co.uk\/?p=534\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-534","post","type-post","status-publish","format-standard","hentry","category-tech-tips"],"_links":{"self":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/534","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=534"}],"version-history":[{"count":1,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/534\/revisions"}],"predecessor-version":[{"id":535,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/534\/revisions\/535"}],"wp:attachment":[{"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xuan.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}