Using PowerShell and Standard Number Formats

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using standard number formats.

Hey, Scripting Guy! Question Hey, Scripting Guy! I love using Windows PowerShell. I use it for everything. My problem is that it does not do everything automatically. For example, I use the Get-Volume cmdlet a lot to check on disk space, but I need to do a manual calculation to see the percentage of free space. This involves basically writing a script and dividing the numbers, multiplying by a hundred, and then trimming all the excess decimal places. Is there anything you can do to help me?

—AV

Hey, Scripting Guy! Answer Hello AV,

Microsoft Scripting Guy, Ed Wilson, is here. This morning I am sitting here sipping a beautiful cup of Darjeeling tea. I added a cinnamon stick to it, and that is it. It is robust, complex, and deeply relaxing. I am also munching on a freshly made bagel, with just a bit of cream cheese on it. I am checking my email on my Surface Pro 3, and looking outside wondering where the snow is. No snow here in Charlotte. Nope, not a flake.

Anyway, on cool, crisp mornings such as this, it is nice to open the Windows PowerShell ISE and curl up with a nice script. However, AV, your problem doesn’t need a scripted solution. You may want to write a quick script, but it is not totally necessary. As someone once said, I can solve that problem in one line.

Display formatted percentages

I can use the Windows PowerShell command Get-Volume (it is technically a function and not a cmdlet) to display a nice output that contains information about my drives. This command and its output are shown here:

Image of command output

One of the properties that appears to be missing is percentage of free space. I like to create a custom property for an object by using the Select-Object cmdlet. To do this, I use a hash table. I specify a label (a name for the property) and an expression (a script block that creates a value for that property). I can use the aliases l for label and e for expression. The hash table looks like this:

@{l='percent free';e={($_.sizeremaining/$_.size)}}

When I do this, I can use the special number format code, “P”, to create my percentage. Basically, it multiplies my value by 100 and returns the first two places after the decimal point. It also rounds up or down as necessary. The number format code becomes an override for the ToString method. In the following example, I show how to call the ToString method. Note that the “P” goes inside the parentheses that are required for all method calls.

PS C:\> (5/10).ToString("P")

50.00 %

To compute the percent free for each of my volumes, I pipe the Get-Volume function to the Select-Object cmdlet, and I specify the volume letter and my hash table as properties. Here is the command I use:

Get-Volume | Select driveletter, @{l='percentfree';e={($_.sizeremaining/$_.size).tostring("P")}}

The command and the output from the command are shown here:

Image of command output

AV, that is all there is to using Windows PowerShell to display a formatted percentage. Numbers  Week will continue tomorrow when I will talk about more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon