PowerShell String Formatting
String formatting builds a finished string from a template and values. The template holds placeholders, and the values fill them in order.
Fill placeholders in order
Put {0} and {1} in the template. The values after -f fill them in order.
"Name: {0}, Age: {1}" -f "Ana", 31Shape numbers and dates
A colon and a specifier shape the value. N2 gives two digits after the point.
"{0:N2}" -f 1234.567
"{0:yyyy-MM-dd}" -f (Get-Date -Date "2026-10-28")Pad values to a width
A width pads the value with spaces. A minus sign pads on the right instead.
"|{0,10}|" -f "Ana"
"|{0,-10}|" -f "Ana"Example
$name = "Ana"
$score = 92.5
"{0,-10} scored {1:N2}" -f $name, $scoreKey points
- Format operator: Use -f to fill placeholders in a template string.
- Placeholders: A placeholder like {0} takes the first value after -f, and {1} takes the second.
- Number format: A specifier after a colon shapes a value, so {0:N2} shows two digits after the point.
- Date format: A specifier like {0:yyyy-MM-dd} shows a date as year, month, then day.
- Padding: A width in a placeholder sets the space the value takes, so {0,10} lines values up on the right.
In this example, the name and the score are stored first. The template pads the name to ten characters on the left, then shows the score with two digits after the point.
Note
A minus sign before the width lines the value up on the left instead.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does "{0:N2}" -f 3.14159 print?