Trying to get the total size of a users mailbox

Mikael Ericsson 1 Reputation point
2022-01-26T20:54:34.447+00:00

I am trying to get the total size of a mail box.

I can get the size of individual folders, for example the Inbox like this:
Get-MgUserMailFolder -UserId $user.Id -All | Where-Object { $_.WellKnownName -eq 'inbox' }
This returns AdditionalProperties.sizeInBytes.

I should be able, I think, to connect to the root folder and get the corresponding value.
Get-MgUserMailFolder -UserId $user.Id -All | Where-Object { $_.WellKnownName -eq 'msgfolderroot' }.
But this returns nothing, no error.
According to https://learn.microsoft.com/en-us/graph/api/resources/mailfolder?view=graph-rest-1.0, msgfolderroot is a wellknownfolder.

I have tried

$folder = Get-MgUserMailFolder -UserId $user.Id -All | Where-Object { $.WellKnownName -eq 'inbox' }
This gives me $folder.ParentFolderId which should be the root.
Get-MgUserMailFolder -UserId $user.Id -All | Where-Object { $
.id -eq $_.ParentFolderId }
Returns nothing.

Is there another way?

Regards
Mikael

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,476 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Glen Scales 4,431 Reputation points
    2022-01-26T23:28:50.52+00:00

    I think the problem is that when you use

    $folder = Get-MgUserMailFolder -UserId $user.Id   
    

    It will start the search at msgfolderroot but it won't include that root folder in the result if you just want to bind to the root folder use

    Get-MgUserMailFolder  
       -MailFolderId <String>  
       -UserId <String>  
    

    where MailFolderId would be the $folder.ParentFolderId you get from the Inbox

    What that cmdlet really needs is another parameter where you can pass in the WellKnownName rather the doing a search and using Where-Object

    That said the AdditionalProperties.sizeInBytes should just be the size of the Root folder not the Mailbox if you want to get true mailbox size then use https://learn.microsoft.com/en-us/graph/api/reportroot-getmailboxusagedetail?view=graph-rest-1.0