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.
New-MgUserMailFolder -UserId <String> -BodyParameter <Hashtable>
You need to pass the new folder displayName to the -BodyParameter.
$params = @{
displayName = "Project Documents"
}
New-MgUserMailFolder -UserId "user@domain.com" -BodyParameter $params
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.
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
}
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
}
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.
Cause: The specified UserId does not exist.
Solution: Verify the UserId and ensure the user exists in the tenant.
Cause: A folder with the same name already exists.
Solution: Check for existing folders with the same name and use a unique name.
Cause: Invalid parameters passed in BodyParameter.
Solution: Ensure the hashtable is correctly formatted and contains valid keys.
Cause: Insufficient permissions to create the folder.
Solution: Ensure the executing account has the necessary permissions to create folders in the specified mailbox.
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.
© m365corner.com. All Rights Reserved. Design by HTML Codex