Formatting multiple text lines

by Apr 18, 2011

Use the awesome formatting operator -f to insert dynamic information into text! You can store the formatting information in a variable and use it in a loop to  format multiple lines of text. Here, the format in $format will tell PowerShell to format the first dynamic information into a six-digit number and to place the second dynamic information in a column nine characters wide and format it as currency:

PS > $format = '{0:D6} {1,9:C}'


PS > (4, 33.20), (12, 8.34), (2, 44.30) | Foreach-Object {'Items      Price'}{ $format -f $_[0], $_[1] }


Items      Price

000004   33,20 $

000012    8,34 $

000002   44,30 $

This sample turns pairs of numbers into a neatly formatted column. You can use Import-CSV to read information from a CSV-file, too.


Twitter This Tip!
ReTweet this Tip!