PowerShell: Using the -F format Operator
When using **-F **format Operator in PowerShell, in the first view it seems like a puzzle but after working with it gets simple.
-F format operator is basically a .NET based.
Why we need to use the -F format operator?
Yes we have few cmdlets by using them we can format the output, but by using -F format operator we can do more than that.
SYNTAX
The syntax for -F format operator is
{<index>[,<alignment>][:<formatString>]}
Format Strings | Description |
C | Currency |
X | Display Number in Hexa Decimal |
p | Display Number as Percentage |
n | Display with width n to left side |
-n | Display with width -n to right side |
dn | Display Numbers Padded by 0 by n times |
# | Digit placeholder, |
, | Thousand separator |
\ | Escape Character |
:ddd | Day of Week |
:dd | Day of Month |
:dddd | Full name of Day of Week |
:hh | Hour |
:HH | Hour in 24 Hour format |
:mm | Minutes |
:SS | Seconds |
:MM | Month in Number |
:MMMM | Name of the Month |
:yy | Year in short |
:yyyy | Full year |
In -F format operator we provide the strings and numbers in the right hand side and stantax for -F in left hand side.
Position
In below examples we are just querying the first number in our right hand side. The first number is 1,
"{0}" -f 1,2,3,4,5
Ok now let's add some space in our first number.
"{0,10}" -f 1,2,3,4,5
You can see that the Position of "1" is moved to little bit on right hand side.
http://lh3.ggpht.com/-NQLkf2woY_A/T03kSpTXj6I/AAAAAAAACXU/9i3UhCsLw84/clip_image002_thumb%25255B1%25255D.png?imgmax=800
- :c or :C | Currency
"{0:C}" -f 200,99,765
You can see that now the output is in currency.
http://lh4.ggpht.com/-ejFZBy3rWuc/T03kU7wbAgI/AAAAAAAACXk/zrogi_6rDBs/clip_image003_thumb%25255B1%25255D.png?imgmax=800
Let's choose the second number
"{1:C}" -f 200,99,765
http://lh4.ggpht.com/-amXArnq5W54/T03kXk-4KcI/AAAAAAAACX0/Dw8Od-ntVLg/clip_image004_thumb%25255B1%25255D.png?imgmax=800
- :x or :X | Hexa Decimal
"{0:x}" -f 909887
It Converted the Number 909887 to hexadecimal value.
http://lh6.ggpht.com/-aZP6Jt56JwU/T03kak6S6II/AAAAAAAACYE/YBJO3fOXxis/clip_image005_thumb%25255B1%25255D.png?imgmax=800
- :p | Percentage
"{0:p}" -f .703
Output is in Percentage.
http://lh5.ggpht.com/--wbhXCaShiY/T03kcqXRv4I/AAAAAAAACYU/cITSxV8Vnfk/clip_image006_thumb.png?imgmax=800
- :dn | Padded 0
"{0:d7}" -f 9
We specified to add 7 zeros but it added 6 , actually it added 7 and the last 0 replaced by the decimal number.
http://lh3.ggpht.com/-PuNdRygXhO0/T03keqmLmDI/AAAAAAAACYk/EaYi8TZI508/clip_image007_thumb.png?imgmax=800
- # | Digit Place Holder
"{0:###-##-##}" -f 8976203
When we use # in format operator the # are replaced by the digits.
http://lh3.ggpht.com/-_91nzLaOkn4/T03kg1KanjI/AAAAAAAACY0/r6iFBvrSMwE/clip_image008_thumb%25255B1%25255D.png?imgmax=800
- Date and Time Formater
"{0:ddd}" -f (get-date)
"{0:dd}" -f (get-date)
"{0:dddd}" -f (get-date)
"{0:hh}" -f (get-date)
"{0:mm}" -f (get-date)
"{0:MM}" -f (get-date)
"{0:MMMM}" -f (get-date)
"{0:yy}" -f (get-date)
"{0:yyyy}" -f (get-date)
http://lh3.ggpht.com/-O7JMqxjGspo/T03kjqjDvYI/AAAAAAAACZE/XYXpTWDFWXU/clip_image009_thumb%25255B1%25255D.png?imgmax=800
- ,| Thousand separator
"{0:#,#}" -f 100980243
http://lh5.ggpht.com/-1k-2XNYFmDs/T03klmtVoxI/AAAAAAAACZU/kNK46SUcuYY/clip_image001_thumb%25255B3%25255D.png?imgmax=800
- Practice Example
$pro = Get-Process
foreach ($a in $pro) {
"{0,20} {1,40}" -f $a.ProcessName , $a.vm
}
Try the above script and the output should be like this .
http://lh6.ggpht.com/-e6WX7Wj4CPA/T03kn2mdRcI/AAAAAAAACZk/woAopRpe_Xk/clip_image001_thumb%25255B4%25255D.png?imgmax=800