Share via


TotalItemSize to GB to 2 Decimal place

Question

Thursday, August 7, 2014 12:25 PM

Hi,

Is it possible to allow the value TotalItemSize to be represented with 2 decimal places. 

Get-Mailbox -ResultSize Unlimited | Sort DisplayName | Select-Object DisplayName, ProhibitSendReceiveQuota, IssueWarningQuota, ProhibitSendQuota, @{label="TotalItemSize(GB)";expression={(Get-MailboxStatistics $_).TotalItemSize.Value.ToBytes()/1GB}}, @{label="ItemCount";expression={(Get-MailboxStatistics $_).ItemCount}}, Database | Export-Csv "C:\Scripts\UserMailboxSizes.csv" -NoTypeInformation

i.e I would like 3.01142540201545 to represented as 3.01 (or 3.00)

Thank you in advance

All replies (11)

Thursday, August 7, 2014 12:53 PM ✅Answered | 2 votes

Are you running this from an EMS shell, or are you using implicit remoting?

The TotalItemSize will be different object types, depending on which method you're using.

If it's the EMS shell:

@{label="TotalItemSize(GB)";expression={'{0:f2}' -f ((Get-MailboxStatistics $_).TotalItemSize.Value.ToBytes()/1GB)}} 

The itemcount/1GB has to be wrapped in parens so it gets evaluated first, otherwise it does the format on the raw byte count, the divides that by 1GB and you've got all the decimals back again.

[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


Thursday, August 7, 2014 12:57 PM ✅Answered | 1 vote

Ah, my typo, there's supposed to be a colon in there:

@{label="TotalItemSize(GB)";expression={"{0:#.##}" -f (Get-MailboxStatistics $_).TotalItemSize.Value.ToBytes()/1GB}}

I hope this post has helped!


Thursday, August 7, 2014 1:14 PM ✅Answered | 1 vote

The expression is just using the format operator to display two decimal places, would it be better to wrap it in the [System.Math]::Round() method, to actually round the number and display it with 2 decimal places?

@{label="TotalItemSize(GB)";expression={[System.Math]::Round((Get-MailboxStatistics $_).TotalItemSize.Value.ToBytes()/1GB)}}

If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

Don't Retire Technet


Thursday, August 7, 2014 1:42 PM ✅Answered | 3 votes

Half-dozen of one, 6 of the other?  Formatting also rounds.

I hope this post has helped!


Thursday, August 7, 2014 4:59 PM ✅Answered | 1 vote

You may try like this - the below i used for logical disk but expression should work

@{Name=”size(GB)”;Expression={“{0:N1}” -f($_.size/1gb)}}, @{Name=”freespace(GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}}

Example :

"{0:n2}" -f ('64993026048'/1GB)

May be like this

Get-Mailbox -ResultSize Unlimited | 
Sort DisplayName | Select-Object DisplayName, ProhibitSendReceiveQuota, IssueWarningQuota, ProhibitSendQuota, @{label="TotalItemSize(GB)";expression={“{0:N2}” -f (Get-MailboxStatistics $_).TotalItemSize.Value.ToBytes()/1GB}}, @{label="ItemCount";expression={(Get-MailboxStatistics $_).ItemCount}}, Database | 
Export-Csv "C:\Scripts\UserMailboxSizes.csv" -NoTypeInformation

Regards Chen V [MCTS SharePoint 2010]


Thursday, August 7, 2014 12:31 PM | 1 vote

I believe you could do it like this:

@{label="TotalItemSize(GB)";expression={"{0:#.##}" -f (Get-MailboxStatistics $_).TotalItemSize.Value.ToBytes()/1GB}}

I hope this post has helped!


Thursday, August 7, 2014 12:50 PM

I believe you could do it like this:

@{label="TotalItemSize(GB)";expression={"{0#.##}" -f (Get-MailboxStatistics $_).TotalItemSize.Value.ToBytes()/1GB}}

I hope this post has helped!

Unfortunately this not work 


Thursday, August 7, 2014 2:55 PM | 1 vote

Actually, looks like format is quicker:

PS C:\temp> (measure-command {1..10000 | %{"{0:#.##}" -f 100.456}}).totalseconds
0.4157004
PS C:\temp> (measure-command {1..10000 | %{[System.Math]::Round(100.456)}}).totalseconds
4.0981795

I hope this post has helped!


Wednesday, March 11, 2015 6:57 AM | 5 votes

Try this :) - worked like a magic

Get-MailboxStatistics -Server YOURSERVERNAME| Sort-Object TotalItemSize -Descending | ft DisplayName,@{label="TotalItemSize(GB)";expression={"$([math]::round($_.TotalItemSize.Value.ToBytes() /1Gb, 2)) GB"}} ,ItemCount  -auto

Mohamed Yousry


Friday, February 26, 2016 5:34 PM | 1 vote

of all the answers in this thread, the ONLY one not marked as an answer to the problem actually Solved my problem!  Thanks Rob!  


Friday, February 26, 2016 8:23 PM | 2 votes

You are most welcome @Ivanmor

Mohamed Yousry