Using New-MgUserMailFolder in Microsoft Graph PowerShell

Managing mail folders is a crucial task for IT administrators. The New-MgUserMailFolder cmdlet in Microsoft Graph PowerShell allows you to create new mail folders in a user's mailbox efficiently. This article covers the cmdlet's syntax, usage examples, tips, use cases, possible errors, and their solutions.


Cmdlet Syntax

New-MgUserMailFolder -UserId <String> -BodyParameter <Hashtable>
  • UserId: The unique identifier of the user. It can be the user's UPN or User ID.
  • BodyParameter: A hashtable containing the details of the new mail folder.

Usage Examples

1. Create a New Folder in the Root Mailbox

You need to pass the new folder displayName to the -BodyParameter.

$params = @{
    displayName = "Project Documents"
}
New-MgUserMailFolder -UserId "user@domain.com" -BodyParameter $params

2. Create a New Folder within an Existing Folder

To create a new folder within an existing folder, you need to pass the parentFolderId (along with the new folder name) to the -BodyParameter.

$params = @{
    displayName = "Invoices"
    parentFolderId = "AQMkAGI2TAAA="
}
New-MgUserMailFolder -UserId "user@domain.com" -BodyParameter $params

Note: You can get the parentFolderId using Get-MgUserMailFolder cmdlet. The Get-MgUserMailFolder cmdlet requires -UserId and -MailFolderId (folder name) to get the parentFolderId.

3. Create Multiple Folders Programmatically

You can also add multiple new folders by storing the folder names in an array and looping through them.

$folders = @("HR", "Finance", "IT")
foreach ($folder in $folders) {
    $params = @{
        displayName = $folder
    }
    New-MgUserMailFolder -UserId "user@domain.com" -BodyParameter $params
}

4. Import Folders from a CSV File

CSV file structure should be as follows:

# Sample CSV content:
# FolderName,ParentFolderId
# Team Meetings,AQMkAGI2TAAA=
# Finance,BQMkBGI3TBBB=
# IT,CQNkCGI4TCCC=


$csvPath = "C:\Path\To\Folders.csv"
$folders = Import-Csv -Path $csvPath

foreach ($folder in $folders) {
    $params = @{
        displayName = $folder.FolderName
        parentFolderId = $folder.ParentFolderId
    }
    New-MgUserMailFolder -UserId "user@domain.com" -BodyParameter $params
}

Verifying Newly Created Folders

You can verify the newly created folders using Get-MgUserMailFolder cmdlet. You have to pass in the UserId or UPN.

Pipe the results to Out-GridView to view the results better as shown in the image.

Cmdlet Tips

  • Use UserPrincipalName: You can use the user's email address as the UserId.
  • Check Existing Folders: Ensure the folder name doesn't already exist to avoid conflicts.
  • Use Parent Folder ID: If creating subfolders, provide the parentFolderId to specify the parent folder.

Use Cases

  • Organizing Mailboxes: Automatically create folders to help users organize their emails better.
  • Project Management: Create project-specific folders for team members.
  • HR Management: Create folders for HR-related documents and communications.
  • IT Administration: Automate the creation of standard folders for new employees.

Possible Errors & Solutions

Error: "Authentication_IdentityNotFound"

Cause: The specified UserId does not exist.

Solution: Verify the UserId and ensure the user exists in the tenant.

Error: "ErrorFolderExists"

Cause: A folder with the same name already exists.

Solution: Check for existing folders with the same name and use a unique name.

Error: "ErrorInvalidArgument"

Cause: Invalid parameters passed in BodyParameter.

Solution: Ensure the hashtable is correctly formatted and contains valid keys.

Error: "ErrorAccessDenied"

Cause: Insufficient permissions to create the folder.

Solution: Ensure the executing account has the necessary permissions to create folders in the specified mailbox.


Conclusion

The New-MgUserMailFolder cmdlet is a powerful tool for managing mail folders in Microsoft 365 mailboxes. By understanding its syntax, usage examples, and potential errors, IT administrators can efficiently organize and manage user mailboxes. Incorporate this cmdlet into your automation scripts to streamline mailbox management and enhance productivity.


Additional Resources:

Microsoft Graph PowerShell Module Documentation
Microsoft Graph API Documentation

Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex