powershellexchange-serverpowershell-2.0exchange-server-2010

Powershell Custom column returning empty value with certain cmdlets


When adding a custom column with the get-mailbox cmdlet i get an empty value.

I'm trying to add a custom column using select @{} on the get-mailbox cmdlet. no matter what i tried the result is always an empty value, i changed the original cmdlet and replaced it with say get-process and then it did work. I even tried with explicitly providing a username and not relying on the pipeline variable, and it didn't work.

get-mailbox <username> | select name, @{name="size"; expression={Get-MailboxStatistics $_.samaccountname | select -ExpandProperty TotalItemSize}}

Thanks in advance for any help.

Edit 1: The reason my question is not the same as Powershell script with Get-Mailbox and Get-MailboxStatistics missing output, as in the mentioned question the person was getting some results from their custom columns, just they were having issues with one row on one column, i don't even get results on the second or third rows.

Edit 2: I know i can create my own object, but i was trying to not to have to, this above code should be working (in a perfect world atleast). also the reason i'm not piping directly, i would've but i was trying to present my question with the least code possible to make it easier for the community to replicate it and to dissect it, the actual code i wanted to run is this

get-mailbox <username> | Get-MailboxStatistics | select displayname,TotalItemSize,@{name="Archive size";expression={Get-MailboxStatistics $_.samaccountname -archive | select -ExpandProperty TotalItemSize}}

My end goal was to get a table with a list of users with their mailbox size and their archive size.

Edit 3: never mind, i tried creating my own object and the same issue persisted. Provided my code used for the object.

 get-mailbox <username> | foreach {[pscustomobject]@{name = $_.name; "mailbox size" = Get-MailboxStatistics $_.samaccountname | select -expand TotalItemSize; "Archive size" = Get-MailboxStatistics $_.samaccountname -archive | select -expand TotalItemSize}}

Thanks again!


Solution

  • Got it. Your code read like this:

    get-mailbox <username> | Get-MailboxStatistics | select displayname,TotalItemSize,@{name="Archive size";expression={Get-MailboxStatistics $_.samaccountname -archive | select -ExpandProperty TotalItemSize}}
    

    has an error. Your expression includes reference to samaccountname which is in Get-Mailbox output but not in Get-MailboxStatistics output, thus you're querying a null mailbox. To fix, query archive mailbox with a displayName attribute.

    get-mailbox <username> | Get-MailboxStatistics | select displayname,TotalItemSize,@{name="Archive size";expression={Get-MailboxStatistics $_.displayname -archive | select -ExpandProperty TotalItemSize}}