Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.

If you own the book already, login here to get free, online, searchable access to the entire book's content.

If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".

5.6 Place Formatted Information in a String

Problem

You want to place formatted information (such as right-aligned text or numbers rounded to a specific number of decimal places) in a string.

Solution

Use PowerShell’s formatting operator to place formatted information inside a string:

PS > $formatString = "{0,8:D4} {1:C}`n"
PS > $report = "Quantity Price`n"
PS > $report += "---------------`n"
PS > $report += $formatString -f 50,2.5677
PS > $report += $formatString -f 3,9
PS > $report
Quantity Price
---------------
    0050 $2.57
    0003 $9.00

Discussion

PowerShell’s string formatting operator (-f) uses the same string formatting rules as the String.Format() method in the .NET Framework. It takes a format string on its left side and the items you want to format on its right side.

In the Solution, you format two numbers: a quantity and a price. The first number ({0}) represents the quantity and is right-aligned in a box of eight characters (,8). It’s formatted as a decimal number with four digits (:D4). The second number ({1}) represents the price, which you format as currency (:C).

Note

If you find yourself hand-crafting text-based reports, STOP! Let PowerShell’s built-in commands do all the work for you. Instead, emit custom objects so that your users can work with your script as easily as they work with regular PowerShell commands. For more information, see Recipe 3.15.

For a detailed explanation of PowerShell’s formatting operator, see “Simple Operators”. For a detailed list of the formatting rules, see Appendix D.

Although primarily used to control the layout of information, the string-formatting operator is also a readable replacement for what is normally accomplished with string concatenation:

PS > $number1 = 10
PS > $number2 = 32
PS > "$number2 divided by $number1 is " + $number2 / $number1
32 divided by 10 is 3.2

The string formatting operator makes this much easier to read:

PS > "{0} divided by {1} is {2}" -f $number2, $number1, ($number2 / $number1)
32 divided by 10 is 3.2

If you want to support named replacements (rather than index-based replacements), you can use the Format-String script given in Recipe 5.17.

In addition to the string formatting operator, PowerShell provides three formatting commands (Format-Table, Format-Wide, and Format-List) that let you easily generate formatted reports. For detailed information about those cmdlets, see “Custom Formatting Files”.

See Also

Recipe 3.15, “Create and Initialize Custom Objects”

“Simple Operators”

“Custom Formatting Files”

Appendix D, .NET String Formatting