Practice Exams for Windows

Practice Exams for Windows

Wednesday, 22 September 2010

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:

image

The Win32_Logical Disk class contains information about volumes and partitions – Exactly what I need.

Output:image

I want to see all the available properties, so I am going to use Format-List * (Alias: fl). Command:

image

Output (sample):

image

I want to select the following information: Hostname, drive letter, label, free space, used space. Command:

image

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:

image

 

That’s not bad, but I really only want whole numbers, no decimal places. I will use .NET formatting to achieve this. Command:

image

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:

image

All done Open-mouthed smile

No comments:

Post a Comment