PowerShell String Formatting
Reports, log lines and file names all need nicely formatted values: two decimals for money, padded columns, dates in a fixed layout. PowerShell gives you three tools: string expansion ("$var"), the format operator -f, and the .ToString() method with a format string.
The -f format operator
Write a template with numbered placeholders {0}, {1}, … on the left, and the values on the right:
$name = 'Linus'
$tasks = 7
'{0} has {1} open tasks' -f $name, $tasks
'{1} tasks for {0}; {0} is busy' -f $name, $tasksLinus has 7 open tasks
7 tasks for Linus; Linus is busy
The same index can appear several times, in any order. This is the .NET composite formatting syntax, so everything you learn here also works in C#.
Number formats
A placeholder can contain a format specifier after a colon: {index:format}.
| Specifier | Meaning | -f 1234.5678 |
|---|---|---|
N2 |
number with separators, 2 decimals | 1,234.57 |
F1 |
fixed-point, 1 decimal | 1234.6 |
C |
currency (culture-dependent) | ¤1,234.57 |
P0 |
percent (value × 100) | for 0.256 → 26 % |
E2 |
scientific | 1.23E+003 |
D5 |
integer padded with zeros | for 42 → 00042 |
X |
hexadecimal | for 255 → FF |
0.00, #,##0 |
custom patterns | 1234.57, 1,235 |
'{0:N2}' -f 1234.5678
'{0:F1}' -f 1234.5678
'{0:P1}' -f 0.256
'{0:D5}' -f 42
'{0:X}' -f 255
'{0:0.00}' -f 3.14159
'{0:#,##0}' -f 9876543
'{0:E2}' -f 1234.56781,234.57
1234.6
25.6 %
00042
FF
3.14
9,876,543
1.23E+003
Currency and some separators depend on the computer's culture (language and region). The verification machine uses the invariant culture; on a German PC N2 would print 1.234,57.
Alignment and padding
{index,width} pads a value to a fixed width: a positive width right-aligns, a negative width left-aligns. Combine with a format: {0,10:N2}.
$items = @(
@{ Name = 'Keyboard'; Price = 49.9 }
@{ Name = 'Monitor'; Price = 189 }
@{ Name = 'Cable'; Price = 4.5 }
)
'{0,-10}|{1,8}' -f 'Item', 'Price'
'-' * 19
foreach ($item in $items) {
'{0,-10}|{1,8:N2}' -f $item.Name, $item.Price
}Item | Price
-------------------
Keyboard | 49.90
Monitor | 189.00
Cable | 4.50
The .PadLeft() and .PadRight() string methods do the same for single values: '7'.PadLeft(3, '0') gives 007.
Dates
Dates have their own specifiers. Use the custom letters for an exact layout (they're case-sensitive: MM is month, mm is minutes):
$launch = Get-Date -Date '2026-09-05 14:07:09'
'{0:yyyy-MM-dd}' -f $launch
'{0:dd/MM/yyyy HH:mm}' -f $launch
$launch.ToString('dddd d MMMM')
$launch.ToString('yyyyMMdd-HHmmss')
Get-Date -Date $launch -Format 'hh:mm tt'2026-09-05
05/09/2026 14:07
Saturday 5 September
20260905-140709
02:07 PM
yyyyMMdd-HHmmss is a popular choice for file names because it sorts correctly as text.
Escaping braces, and a common bug
To print a literal brace in a -f template, double it: {{ and }}. And remember that -f has higher precedence than most operators but lower than a method call, so wrap complex values in parentheses:
'{{"id": {0}}}' -f 17
'Total: {0}' -f (2 + 3)
'Sum of {0} and {1} is {2}' -f 2, 3, (2 + 3){"id": 17}
Total: 5
Sum of 2 and 3 is 5
Choosing a technique
| Need | Use |
|---|---|
| Simple sentence with variables | "Hello $name" |
| Expression in text | "Total: $($a + $b)" |
| Fixed decimals, padding, columns | '{0,8:N2}' -f $x |
| One value in a custom format | $x.ToString('0.0'), $date.ToString('yyyy-MM-dd') |
| Multi-line template | here-string + -f or expansion |
Note:
-freturns a string. If you format numbers for display, do the maths first and format at the end; formatted values can't be summed or sorted numerically any more.
Test yourself
What does '{0:N1}' -f 2.345 produce (invariant culture)?
- 2.3
- 2.345
- 2.35
- 2
What does the width in {0,-12} do?
- Right-aligns in 12 characters
- Left-aligns in 12 characters
- Cuts the value to 12 characters
- Adds 12 spaces before
In a date format, what is the difference between MM and mm?
- None
- MM is month, mm is minutes
- MM is minutes, mm is month
- mm is milliseconds