GraphAPI – Creating a Mailbox Folder via PowerShell

Scenario: You want to create mailbox folders using GraphAPI via PowerShell

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.

Scriptlets:

#1. Build $MBX
$mbx = "steve@steveman.com"

#2. Build your AppURI
$appuri = "https://graph.microsoft.com/v1.0/users/$mbx/mailfolders"

#3. Build your $params to include the new Folder Name and if it should be hidden
                $params = @{
        	         DisplayName = "TestGraphAPI"
	                 IsHidden = $false
                     } | ConvertTo-Json

#4. Build your RestSplat package
#Build your URI Package to POST
                $RestSplat = @{ 
                    URI         = $appuri
                    Headers     = $(get-accesstoken)
                    Method      = 'POST' 
                    ContentType = "application/json" 
                    Body = $params 
                }        

#5. Execute the RestSplat package
Invoke-RestMethod @RestSplat                       
Advertisement

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: