GraphAPI – Move messages in a mailbox folder to another folder

Scenario: You want to move messages from one mailbox folder to another

Prerequisites: You already have the following configured:
Registered App in Azure: An Azure Registered app to connect to with Application Permissions for Mail.Read, Mail.ReadWrite, Mail.ReadBasic.All, and Mail.Send. (Not all permissions listed may be necessary for the specific function below)
Bearer Authentication Token: A method for pulling back a bearer token and storing it to pass Authorization into your RestAPI package (See my previous post about get-accesstoken). The get-accesstoken authenticates against your Registered App in Azure.


#1. Build $mbx
$mbx = ""

#2. Build your $AppURI of the source location of the mailbox folder items
#Multiple Examples:
$appuri = "$mbx/mailfolders/inbox/Messages"   

$appuri = "$mbx/mailfolders/AAMkADY3ODhkMDNlLTRjM2YtNDJjYi04YTkzLTFmNjFhYTcyMjA2NwAuAAAAAAAM5kRh6DylRZ4XsImGGVClAQA0fvQp6d6ET6JezCAd8xOWAAFrER8_AAA=/Messages"
$appuri = "$mbx/mailfolders/RecoverableItemsDiscoveryHolds/Messages"   
$appuri = "$mbx/mailfolders('RecoverableItemsPurges')/Messages"

#3. Find All messages in that source folder
 $messages = @()
                    "Running $appUri"
                    $RestSplat = @{
                        URI = $appuri
                        Headers = $(get-accesstoken)
                        Method = 'GET'
                        ContentType = "application/json"
                    $Tempresults = Invoke-RestMethod @RestSplat
                    $messages += $tempresults.value
                    $appuri = $tempresults."@odata.NextLink"
                    }While($appuri -ne $null)

                #Display your Messages if you want to see

#4. Move the messages you found in the step above to the destination folder
#Build a $Params Variable with the folderID we want to pass to
#You can use a Base64 ID
$params = @{
	                    DestinationId = "AAMkADY3ODhkMDNlLTRjM2YtNDJjYi04YTkzLTFmNjFhYTcyMjA2NwAuAAAAAAAM5kRh6DylRZ4XsImGGVClAQA0fvQp6d6ET6JezCAd8xOWAAFrER8_AAA="
                    } | Convertto-Json

#Or use the name of a common folder 
                    $params = @{
	                    DestinationId = "Inbox"
                    } | Convertto-Json

#Move the Messages
            $c = 0
            $T = $
            $ | %{
                $m = $_
                "$c / $t : Moving $M"
                $appuri = "$mbx/messages/$m/move"
                        $RestSplat = @{ 
                            URI         = $appuri
                            Headers     = $(get-accesstoken)
                            Method      = 'POST' 
                            ContentType = "application/json" 
                            Body = $params 
                $move = Invoke-RestMethod @RestSplat 

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: