PowerShell formatting strings

Hello everybody,

today I want to write few words about formatting strings in PowerShell.

Consider the following example:

"Today is $(Get-Date). Enjoy your life"

Output will be the following:

Today is 08/23/2016 16:04:18. Enjoy your life

So, if you want to call some PowerShell function, you can just put it into brackets, put $ in front of it and get the result.

Conisder another example:

"You own to me $(44 + 44* 0.15) dollars"

It will give following output:

You own to me 50.6 dollars

Very cool stuf in PowerShell is about using formatting with templating and key -f.

Take a look:

$apples = 5

$pomegranade = 22

cls

"I have {0} apples and {1} pomegranades" -f $apples, $pomegranade

How do you feel, what will be the output of that program?

here it is:

I have 5 apples and 22 pomegranades

-f is something that says to PowerShell, please apply formatting to preceding string.

Of course, PowerShell can format numbers as well.

Take a look at following PS code:

$num = 9876543210.123456789

"N0 {0:n0} formatting" -f $num

"N1 {0:n1} formatting" -f $num

"N2 {0:n2} formatting" -f $num

"N3 {0:n3} formatting" -f $num

"N0 special formatting {0,20:n0} formatting" -f $num

I hope you've got the pattern.

if you write in curly brackets 0 and then nX, then PS will print x numbers after comma. 

But if you write {x,y:n0} then PS will do the following, it will take the variable, remove numbers after comma, and tries to make of of length y. Below output may help you to grasp clearer:

N0 9,876,543,210 formatting

N1 9,876,543,210.1 formatting

N2 9,876,543,210.12 formatting

N3 9,876,543,210.123 formatting

N0 special formatting        9,876,543,210 formatting

As you can see, N0 special formatting cutted numbers after dot, added comas, and also !!! added aligning, to make string of length 20.

If anybody speaks about numbers, quite often they need to calculate not just apples, pomegranades, but also need to calculate money. And for money special formatting character is intended. It is C character, which stands for currency.

Take a look at code below:

"C0 {0:C0} formatting" -f $num

"C0 {0:C1} formatting" -f $num

"C0 {0:C2} formatting" -f $num

"C0 {0:C3} formatting" -f $num

Try to imagine, what will be the output? Ready?

here it is:

C0 $9,876,543,210 formatting

C0 $9,876,543,210.1 formatting

C0 $9,876,543,210.12 formatting

C0 $9,876,543,210.123 formatting

As you can see, PS reads from system what is default currency at my system, and adds it to formatting. In my case it is $.

PS takes care not only about money. It also takes care about percentages.

Notice following sample:

$percent = 0.1245

"P0 {0:P0}" -f $percent

"P0 {0:P1}" -f $percent

"P0 {0:P2}" -f $percent

If you suppose, that it will print you zeros, or something like this, then notice the output:

P0 12 %

P0 12.5 %

P0 12.45 %

As you can see it multiplies variable at 100, and adds sign of % to the end. 

Now let's think about programmers. If you are programmer, you will think not only in decimal format, but also in hexadecimal format as well. Look at hexadecimal formatting in PowerShell. And you can pass into formatting of any kind. Decimal or hexadecimal. Take a look:

"X0 0x{0:x0} formatting" -f 69

"X0 0x{0:x0} formatting" -f 0x45

And as always here is the output:

X0 0x45 formatting

X0 0x45 formatting

One more string specifier is for decima numbers. It is named by D.

Take a look at the following code:

$dec = 1234567890

"D0 {0:D0}" -f $dec

"D8 {0:D12}" -f $dec

"D0 {0:D0}" -f $dec

"D0 {0,12:D0}" -f $dec

below goes output from it:

D0 1234567890

D8 001234567890

D0 1234567890

D0   1234567890

Take not that "D8 {0:D12}" -f $dec added zeroz in begining of string, while "D0 {0,12:D0}" -f $dec added spaces in begining. 

Other parts of formatting I'll cover in other article.

No Comments

Add a Comment