Create Custom Folder in Exchange Online Mailboxes using Graph API using Windows PowerShell

I am hear by explaining the step-by-step procedure to create Custom Folder for all Exchange Online Mailboxes using Graph API. Using the cmdlet New-MailboxFolder, Administrators can’t create folders in other mailboxes and use an alternate option like Graph API, when there is a special requirement to access all the mailboxes in your Exchange Online Organization.


  • Windows PowerShell 5.1
  • Azure tenant and Exchange Online account Access
  • App registration permissions on the subscription

Create an Application Identity in Azure

In this example, I am explaining a method in which, you access the Microsoft Graph API with an OAuth token.

The following steps with help to create an application Identity in your Azure Tenant with privileges to access the Exchange Online Mailboxes
1. Login Azure Portal and search for ‘Application Registrations’ as below:

2. Register an Application

3. Name your application Registration and follow the below settings

4. Application Details can be noted from the following window

5. Create Application Secret from the following page and note the secret up on completion

6. Assign API permissions to access your Exchange Online mailboxes from the following window

We need to select Graph ‘Application permission’ for the purpose of mailbox access as below:

Select the following permissions and save

7. Grand Admin Consent to the selected permissions in the tenant. Finally, your permissions page will show as follows

8. Now we are ready to run the PowerShell cmdlet.

  • Copy the cmdlet to a notepad and save as .ps1 file
  • Edit the following parameters as per your tenant settings
    • $Mailboxes = @(“”,””)
    • $Folders = @(“folder1″,”folder2”)
    • $AppId = ‘Your App ID’
    • $AppSecret = ‘Your app secret’
    • $TenantName = “”
##Create Custom Folder for Exchange Online Mailboxes using Graph API

$Mailboxes = @("","")
$Folders = @("folder1","folder2")
$AppId = 'Your App ID'
$AppSecret = 'Your app secret'
$Scope = ""
$TenantName = ""
$Url = "$TenantName/oauth2/v2.0/token"
# Add System.Web for urlencode
Add-Type -AssemblyName System.Web
# Create body
$Body = @{
    client_id = $AppId
	client_secret = $AppSecret
	scope = $Scope
	grant_type = 'client_credentials'
# Splat the parameters for Invoke-Restmethod for cleaner code
$PostSplat = @{
    ContentType = 'application/x-www-form-urlencoded'
    Method = 'POST'
    # Create string by joining bodylist with '&'
    Body = $Body
    Uri = $Url
# Request the token!
$Request = Invoke-RestMethod @PostSplat
# Create header
$Header = @{
    Authorization = "$($Request.token_type) $($Request.access_token)"
## Access Mailboxes

foreach($mailbox in $Mailboxes) {
    $Uri = "$mailbox/mailFolders"
    ## Fetch Folder Names
    $Mailboxfolders = Invoke-RestMethod -Uri $Uri -Headers $Header -Method Get -ContentType "application/json"
    $MailboxfoldersList = $Mailboxfolders.Value.Displayname
    $NextPage = $Mailboxfolders.'@Odata.NextLink'
    While($NextPage -ne $Null) {
        $Mailboxfolders = Invoke-RestMethod -Uri $NextPage -Headers $Header -Method Get -ContentType "application/json"
        $MailboxfoldersList += $Mailboxfolders.Value.Displayname
        $NextPage = $Mailboxfolders.'@Odata.NextLink'

## Loop folders
    foreach($Folder in $Folders) {
$Body = @"
"displayName": "$Folder"
        ## Show Progress
        Write-Host "Checking the folder with similar name..."
        if($($MailboxfoldersList) -contains $folder) {
            write-host "$folder already exists. Unable to create the folder..."
        else {
            $Newfolder = Invoke-RestMethod -Uri $Uri -Headers $Header -Method Post -Body $Body -ContentType "application/json"
            write-host "Created Folder: $($Newfolder.displayName) in mailbox $mailbox...`n"

9. Run the script. Open a PowerShell and run as below:

As you can see from the report above, folders are being created and you may check this from your mailboxes too.

Hope, the above article help you a lot in managing your Exchange Online mailbox’s folder creation !!!