Hashtable Expression Unit format

Hello how to format the expression like : Number+ GB



Get-NetAdapterStatistics| Select-Object Name, @{Name="RE"; Expression={ [System.Math]::Round($_.ReceivedBytes/1GB)}}


You could create it as a string in the Select-Object custom property or use Format-Table in the format specifier.

    Select-Object Name, 
                  @{Name=“RE”; Expression={"$([system.math]::Round($_.ReceivedBytes/1GB)) GB"}}

    Format-Table  Name, 
                  @{Name=“RE”; Expression={$_.ReceivedBytes/1GB};f="0' GB'"}

What does it mean? $ before system:: round?

It prepare a string with dollars?

And this:.

f="0’ GB’

$() is the sub-expression operator. It is useful when evaluating an inner expression inside of a string. f= is short for formatString=. The 0 is any element returned by the expression. GB is just a string.

In double-quotes expressions proceeded by $ are evaluated and the result is inserted into the string. So in

"$([system.math]::Round($_.ReceivedBytes/1GB)) GB"

The subexpression


is evaluated and it’s result is placed in the string. See about_quoting_rules documentation for more info.

Here’s some more references:
Get-Help About_Calculated_Properies:
Formatting types in .Net

Get-Help About_Operators

1 Like

So what else can use in hashtable more? Name, expression, format-table ? and more?

In this case use them for calculated properties which can be used in Select-Object, Compare-Object, Group-Object, Measure-Object, Sort-Object, CovertTo-HTML and all the Format-* cmdlets. See about_calculated_properties. In PS 3.1 in greater you can also use them to create custom objects using the [pscustomobject] type accelerator (see about_object_creation and about_type_accelerators).

1 Like