Search notes:

PowerShell: -f operator

The -f operator formats strings.
-f is a shorthand form for the String.Format function.

Syntax

The -f operator is a binary operator with the format string on the left side and the values (array) to be formatted on its right side:
'format string' -f array
In the format string, curly braces denote the locations where the elements of the array will be formatted.
In the simplest form, such curly braces contain the index of the element in the array that will be formatted. Note: in the following example, the second element is printed first with other two elements following:
PS C:\> '{2} {0} {1}' -f 'zero', 'one', 'two'
two zero one
Specifing width and alignment
The width of the formatted string can be controlled by separating the position number from the width by a comma. This allows for nicely formatted tables with numbers:
foreach ($i in  5,111,71) {
foreach ($j in 42,321, 9) {
  '{0,3} * {1,3}   = {2,5}' -f ($i, $j, ($i*$j))
}}
  5 *  42   =   210
  5 * 321   =  1605
  5 *   9   =    45
111 *  42   =  4662
111 * 321   = 35631
111 *   9   =   999
 71 *  42   =  2982
 71 * 321   = 22791
 71 *   9   =   639
In order to left align a value, the width needs to be prepended by a minus sign:
foreach ($i in ('five', 5), ('forty-two', 42), ('three', 3)) {
  ' {1,2}: {0,-9}' -f $i
}
  5: five
 42: forty-two
  3: three
A formatting instruction can be given with the colon. For example, :x formats a number as hexadecimal (compare with format-hex). Formatting instructions can be specified with or without a width:
(3,255,2048).foreach( { '{0,3:x} = {0,4} | {0:x} = {0}' -f $_ } )
  3 =    3 | 3 = 3
 ff =  255 | ff = 255
800 = 2048 | 800 = 2048
Leading zeros can be specified as follows
(1,23,456,7890).forEach( { '{0,6:00000}' -f $_ } )
 00001
 00023
 00456
 07890
Alternatively, leading zeroes can also be specified by indicating the desired number of places (here: 5) after the formatting instruction (here: d):
(1,23,456,7890).forEach( { '{0,6:d5}' -f $_ } )
 00001
 00023
 00456
 07890
When formatting hexadecimal numbers, it is x followed by the maximum width to be filled with zeroes:
'{0,4:x4} {1,4:x4}' -f 1024, 4095
0400 0fff

Positional arguments and alignment

The following script demonstrates the positional arguments and alignment:
echo ("0: {0} - 2:{2} - 1: {1}" -f 'zero', 'one', 'two')
#
#  0: zero - 2:two - 1: one

#
# {x, n) right aligns the xth element with n places
#
# {x,-n) left aligns the xth element with n places
#
forEach ($item in ('five', 5), ('a hundred', 100), ('two', 2), ('forty-two', 42) ) {
  echo ("{0,-10}: {1,3}" -f $item[0], $item[1])
}
#
#  five      :   5
#  a hundred : 100
#  two       :   2
#  forty-two :  42
Github repository about-Powershell, path: /language/operator/string-manipulation/f/positional-arguments-and-alignment.ps1

Escape curly braces

Because curly braces ({ and }) have a special meaning when used with the -f operator, there needs to be a way to escape that meaning in order to have curly braces in the created string (for example when used to create JSON objects).
In order to escape a curly brace, it needs to be doubled:
'{{"message": "{0}", "data:{1} }}' -f 'foo', 'bar'

Formatting a System.DateTime object

A System.DateTime object can be formatted with -f like so:
$dt = new-object dateTime 2021,03,17 , 14,09,53

write-host "The value of dt is $('{0:yyyy-MM-dd HH:mm:ss}' -f $dt)"
Github repository about-Powershell, path: /language/operator/string-manipulation/f/DateTime.ps1

Hexadecimal representation of a number

The -f operator allows to quickly get a hexadecimal representation of a number:
'{0:x}' -f 65535

See also

.NET: format strings
operators
printf.

Index