Custom columns and table entries with Format-Table

Sometimes objects in PS don’t have a descriptive property name or the information of an object could use a little bit of formatting to better fit our purposes. Take for example the VirtualMemorySize64 property of the processes running on Windows:

PS C:\Windows\system32> Get-Process | Format-Table Name, VirtualMemorySize64
Output of Get-Process | Format-Table Name, VirtualMemorySize64
Output of Get-Process | Format-Table Name, VirtualMemorySize64

That’s a big number. We can dig a bit further to check its type to have a better idea of what we’re dealing with:

PS C:\Windows\system32> Get-Process | Select-Object -Property VM | Get-Member
Output of Get-Process | Select-Object -Property VM | Get-Member
Output of Get-Process | Select-Object -Property VM | Get-Member

With the type name (System.Diagnostics.Process) we can check in the .NET MS documentation to find that this property shows “the amount of the virtual memory, in bytes, allocated for the associated process”. Now we know why it shows such a big number. But what if we want to see MB or GB instead of bytes? Here’s when the formatting options of Format-Table can be handy for table entries (will see how this works for columns in a bit).

Creating custom table entries

To modify the value of an object’s property we have to tell PS what we want to output to the host and what’s the corresponding name of the property in the object that’s being piped to Format-Table. If we want to see the value of VirtualMemorySize64 in megabytes we can use the following syntaxis:

Get-Process | Format-Table Name, @{name='VM64 in Megabytes'; expression {$_.VirtualMemorySize64 / 1MB}}
Output of Get-Process | Format-Table Name, @{name='VM64 in Megabytes'; expression {$_.VirtualMemorySize64 / 1MB}}
Output of Get-Process | Format-Table Name, @{name=’VM64 in Megabytes’; expression {$_.VirtualMemorySize64 / 1MB}}

By adding formatstring (or just -f wil work too) with F2 value (fixed point number with 2 decimals) we get a cleaner result:

Output of Get-Process | Format-Table Name, @{name='VM64 in Megabytes'; expression {$_.VirtualMemorySize64 / 1MB}; f='F2'}
Output of Get-Process | Format-Table Name, @{name=’VM64 in Megabytes’; expression {$_.VirtualMemorySize64 / 1MB}; f=’F2′}

MB means megabytes and it tells PS that you want to convert bytes to megabytes.

Creating custom table property names

We already did this for table entries. We can use the same syntax too:

Get-Process | Format-Table Name, @{name='Hey! Its me, VM64'; expression={$_.VirtualMemorySize64 / 1MB}}
Output of Get-Process | Format-Table Name, @{name='Hey! Its me, VM64'; expression={$_.VirtualMemorySize64 / 1MB}}
Output of Get-Process | Format-Table Name, @{name=’Hey! Its me, VM64′; expression={$_.VirtualMemorySize64 / 1MB}}

You can include additional custom properties by following the usual syntax to separate the properties that Format-Table shows (a comma):

Get-Process | Format-Table Name, @{name='Hey! Its me, VM64'; expression={$_.VirtualMemorySize64}}, @{name='Name again'; expression={$_.Name}}
Output of Get-Process | Format-Table Name, @{name='Hey! Its me, VM64'; expression={$_.VirtualMemorySize64}}, @{name='Name again'; expression={$_.Name}}
Output of Get-Process | Format-Table Name, @{name=’Hey! Its me, VM64′; expression={$_.VirtualMemorySize64}}, @{name=’Name again’; expression={$_.Name}}

| Theme: UPortfolio