Search
Joe Pruitt - A Software Architect's take on Network Security
You are here: DevCentral > Weblogs

posted on Friday, December 19, 2008 10:27 AM

PowerShell_2 Welcome to this addition of the PowerShell ABC's where you'll find 26 posts detailing a component of the PowerShell scripting language, one letter at a time.  Today's letter is the letter "F".  For "F" I've picked the word that relates to how you can make nice and pretty output.  Today's word is the Format operator (-f).

Most often, PowerShell's build in output formatting is good enough for your needs.  But, there will always be those times when you want a more fine grained control over the formatting of your output.  You may also want to format strings in a specific way.  Enter the format operator "-f".

dos_formatThe format operator -f is a binary operator that takes a format string as it's left operand and an array of values to format as it's right operand.

PS> "{2} {0}" -f "there", "ignore", "hi"

hi there

In the format string, the values enclosed in braces correspond to the index of the element on the right operand array.  Along with reordering, you can also control how the array values are laid out.  It behaves very similar to pythons "%" operator.  The format operator is a shorthand for calling the .NET Format method in the System.String class.

The basic format for the index specifiers is

(<index>[,<alignment>][:<formatString>])

This is best illustrated with a couple of examples.  Here is a table taken from PowerShell in Action by Bruce Payette:


Operator Example Results Description

{0} Display a particular element "{0} {1}" -f "a", "b" a b
{0:x} Display a number in Hexadecimal "0x{0:x}" -f 181342 0x2c45e
{0:X} Display a number in Hexadecimal uppercase "0x{0:X}" -f 181342 0x2C45E
{0:dn} Display a decimal number left justified, padded with zeros "{0:d8}" -f 3 00000003
{0:p} Display a number as a percentage "{0:p}" -f .123 12.30 %
{0:c} Display a number as currency "{0:c}" -f  12.34 $12.34
{0,n} Display with field width n, left aligned "|{0,5}|" -f "hi" |   hi|
{0,-n} Display with field width n, right aligned "|{0,-5}| -f "hi" |hi   |
{0:hh}
{0:mm}
Display the hours and minutes from a date time value "{0:hh}:{0:mm}" -f (Get-Date) 01:34
{0:C} Display using the currency symbol for the current culture "|{0,10:C}|" -f 12.3 |    $12.40|

There are a lot more things you can do with formatting than are illustrated in this table.  For more information, check out the Formatting Overview on MSDN.



Feedback

7/1/2009 11:24 AM
Gravatar FYI: There's an error in the example from the table, 8th line: It should be -- "|{0,-5}|" -f "hi"
Rod
7/1/2009 12:40 PM
Gravatar Good catch! Darn fat fingers... B-).

-Joe
Joe Pruitt
4/15/2011 12:31 PM
Gravatar And it looks like your table has its labels arranged in the wrong order - isn't that Operator, Description, Example, Result?
Mike Hammond

Let Me Know What You Think


Please use the form below if you have any comments, questions, or suggestions.

Title:
 
Name:
 
Email: (so we can show your gravatar)
Website:
Comment: Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, code
 
Please add 6 and 6 and type the answer here:

Blog Stats

Posts:379
Comments:1067
Stories:1
Trackbacks:301
  

Article Categories

  iRules
  

Image Galleries

  

Joe's bookshelf: read

The Lost Gate
4 of 5 stars
This one started slow but I got really got into it about 1/3 of the way through. If you are an Ender's Game fan, you'll probably like this one as well.

goodreads.com


82,243 Members in 102 Countries and Growing!

Join DevCentral Today!

About DevCentral

DevCentral has been a successful, thriving community for many years. We have always strived to bring you the best technical documentation, discussion forums, blogs, media and much more that we can.

So dive in, get familiar with DevCentral. We hope you like it, we hope it makes your job easier, and lets you get that much more power out of the community. To learn more, make sure to check out the Getting Started section. And if you have any problems, or think something could be easier to use, drop us a line to let us know.

Got It !

We've received your comment and transmitted it directly to DevCentral HQ.

Thanks for taking time to let us know what's on your mind. At DevCentral | Community Matters!

Get In Touch With Us

Have questions, suggestions or just want to get something off your chest?

Use our handy form below to Direct Connect with DevCentral Mission Control.

Send Us Feedback       or