Windows PowerShell - Calculating and Formatting Values of Properties
This example will show you how to calculate the free and used disk space percentages and remove decimal places from the output.
I’m going to start by getting some basic WMI information, command:
The Win32_Logical Disk class contains information about volumes and partitions – Exactly what I need.
I want to see all the available properties, so I am going to use Format-List * (Alias: fl). Command:
Output (sample):
I want to select the following information: Hostname, drive letter, label, free space, used space. Command:
Explanation:
- The back ticks ` at the end of each line allow me to continue the command on the next line down.
- The @{} syntax is an expression to create a calculated member and/or rename the member.
Output:
That’s not bad, but I really only want whole numbers, no decimal places. I will use .NET formatting to achieve this. Command:
Explanation:
- The "{0:N0}" –f syntax specifies to set the format to Numeric with 0 decimal places. The first 0 is the index and can be left at zero. Set the second zero to a different number to change the number of decimal places.
Output:
All done