Export (0) Print
Expand All
Expand Minimize

Windows PowerShell Tip of the Week

Windows PowerShell Tip of the Week


Here’s a quick tip on working with Windows PowerShell. These are published every week for as long as we can come up with new tips. If you have a tip you’d like us to share or a question about how to do something, let us know.

Formatting Numbers

One of the … great … things about system administration is the opportunity to work with huge, practically-unreadable numbers. For example, when dealing with the WMI class Win32_LogicalDisk one of the properties you can use is FreeSpace, a property that returns the number of available bytes on a disk drive. On one of the Scripting Guys’ test computers, that happens to be this number:



19385790464



So what exactly is that number? Well, let’s see, it’s … we think it’s … if you put a comma there it’s – well, OK we admit it: we have no idea. It’s a big number, but that’s about all we know.

The fact of the matter is that computers don’t need – and don’t want – nicely-formatted, easy-to-read numbers. For computers users, however, it’s a very different story. Numbers like 19385790464 aren’t very useful to us; far more useful are numbers like this:



19,385,790,464



As you can see, once you insert a few commas it’s much easier to tell that we have a little more than 19 billion bytes of free disk space on this computer. The moral of the story is obvious: if you want output that’s easy to read then you need to format your numbers before displaying them. There’s just one little problem here: how exactly do you go about formatting your numbers before displaying them?

If we were using VBScript, this would be easy: we’d simply call the FormatNumber function and have at it. Of course, we aren’t using VBScript; we’re using Windows PowerShell. As near as we can tell, PowerShell doesn’t include any built-in functions or cmdlets that enable us to format numbers. Which would seem to indicate that we’re just plain out of luck here.

Using .NET to Format Numbers in Windows PowerShell

Or are we? It’s true that Windows PowerShell doesn’t have any built-in functions or cmdlets for formatting numbers. But that’s OK; we don’t need any built-in functions or cmdlets. Instead, we can use the .NET Framework formatting methods. For example, consider this simple little PowerShell script:



$a=19385790464
"{0:N0}" -f $a



What are we going to get back when we run this script? This is what we’re going to get back:



19,385,790,464



In other words, we’re going to get back one of those nicely-formatted numbers we’ve all been wishing for.

So how exactly did we do that? To answer that question, let’s take a closer look at our formatting command. The heart-and-soul of our command is this little construction: “{0:N0}”. That’s a crazy-looking bit of code to be sure, but it’s also a bit of code that can easily be dissected:

  • The initial 0 (the one that comes before the colon) represents the index number of the item to be formatted. For the time being, leave that at 0 and everything should work out just fine.

  • The N represents the type of format to be applied; in this case, the N is short for Numeric. Are there other types of formats we can apply? Yes there are, and we’ll show you a few of those in just a moment.

  • The second 0 (the one after the N) is known as the “precision specifier,” and, with the Numeric format, indicates the number of decimal places to be displayed. In this case we don’t want any decimal places, so we set this parameter to 0. Suppose we wanted to display three decimal places? No problem; this command takes care of that: "{0:N3}" -f $a. Run that command and you’ll end up with output that looks like this: 19,385,790,464.000.

That’s about all we have to do; after specifying the format type we tack on the –f (format) parameter, then follow that by indicating the value we want to format ($a). Are we limited to formatting variables? Of course not; you can format a literal value if you want to:



"{0:N2}" -f 554.22272



Give that command a try and see what happens.

One thing to keep in mind here is that the formatting commands merely control the way a value is displayed; the value itself doesn’t change. For example, consider this simple little script



$a = 5.136
"{0:N0}" -f $a



If we run this script the value 5 will be displayed onscreen; however, if we later display the value of the variable $a we’ll get this:



5.136



If you want to change the actual value of $a you’ll need to assign the formatted number to a variable (like, say, $a):



$a = 5.136
$a = "{0:N0}" -f $a



Now if you display the value of $a you’ll get this:



5



More Fun With Formatting Numbers

As we noted earlier, there are other formats that can be applied to numbers. We won’t discuss these in any detail, but in addition to the Numeric format you can apply the following format types to a number:

Name

Specifier

Description

Currency

C

The value is displayed as currency, using the precision specifier to indicate the number of decimal places to be displayed.

Decimal

D

The value is displayed using the number of digits in the precision specifier; if needed, leading zeroes are added to the beginning of the number.

Percentage

P

The value is multiplied by 100 and displayed as a percentage. The precision specifier indicates the number of decimal places to be displayed.

Hexadecimal

X

The value is displayed as a hexadecimal number. The precision specifier indicates the number of characters to be displayed, with leading zeros added to the beginning as needed.


To show you how these different formats work, try running the following script:



$a = 348 

"{0:N2}" -f $a
"{0:D8}" -f $a
"{0:C2}" -f $a
"{0:P0}" -f $a
"{0:X0}" -f $a



Here’s the output you should get back:



348.00
00000348
$348.00
34,800 %
15C



Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft