How-to: The -f Format operator

Format a string expression.

Syntax:
       "String with placeholders" -f "Array of values to place into the placeholders"

       'Filename: {0} Created: {1}' -f $_.fullname,$_.creationtime

       "{I,A:FS} {I,A:FS} {I,A:FS}.." -f "string0", "string1", "string2"...

Place {0} {1} etc. into the string as placemarkers where you want the variables to appear, immediately follow the string with the -f operator and then lastly, a list of comma separated variables/objects which will be used to populate the placemarkers.

Key:
   I   Index of the item to display, 0,1,2 etc.
   A   Alignment. 
       A positive number will right align n characters. 
       A negative number will left align n characters. 
        so {2,-25} will allocate 25 characters of horizontal space on the line, even if the string is only 1 character long. 
   FS  An optional Format String that acts on the item depending on its type (not case sensitive).

       Valid format strings:
:c Currency format (for the current culture)
:d Padded. (:dP precision=number of digits); if needed, leading zeros are added to the beginning of the (whole) number.
:e Scientific (exp) notation
:f Fixed point
:f5 = fix to 5 places
:g Most compact format, fixed or sci
:g5 = 5 significant digits
:n Number (:nP precision=number of decimal places), includes culture separator for thousands 1,000.00
:p Percentage
:r Reversible Precision
:x Hex format
:hh
:mm
:ss
Convert a DateTime to a 2 digit Hour/minute/second
"{0:hh}:{0:mm}"
:HH Hour in 24 Hour format
:dd Day of Month
:ddd Convert a DateTime to Day of the Week
:dddd Full name of Day of Week
:yyyy Full year
# Digit Place Holder

Some of these letters (:d, :D, :f, :F, :g, :G and :r, :R) also perform double duty as date patterns which can lead to some confusion.
When applied to a date they will act as date patterns and they are then case sensitive: Full list of Date Formats

The -f format string can be surrounded by either single or double quotes.

Static text or more complex expressions may be included before or in-between the -f {format strings}
The -F operator has equal precedence with Arithmetic operators, * / % + - etc, see About_Operator_Precedence
When operators have equal precedence, PowerShell evaluates them from left to right.

Examples:

Display a number to 3 decimal places:

PS C:\> "{0:n3}" -f 123.45678
123.457

Right align the first number only:

PS C:\> "{0,10}" -f 4,5,6
         4

Measure the size of a folder and display in GB rounded to 2 decimal places:

PS C:\> "{0:N2} GB" -f ((Get-ChildItem C:\Work\ -Recurse -Force | Measure-Object -property length -sum).sum / 1Gb)
12.45 GB

Left and right align text:

PS C:\> "|{0,-10}| |{1,10}|" -f "hello", "world"
|hello     ||     world|

Display an integer padded with 3 digits:

PS C:\> "{0:d3}" -f [int32]12
012

Separate a number with dashes (# digit place holder):

PS C:\> "{0:###-##-##}" -f 1234567
123-45-67

Create a list of 100 names with a padded suffix no. (Name001 → Name100):

PS C:\> 1..100 | % { 'Name{0:d3}' -f $_ }

Convert a number to Hex:

PS C:\> "{1,10} {0,10} {2,10:x}" -f "First", "Second", 255
    Second     First        FF

Convert the character 'A' to its hex equivalent ([int][char] gives the Ascii number and then we convert):

PS C:\> '0x' + "{0:x}" -f [int][char]'A'
0x41

Display filenames and creation time:

PS C:\> Get-ChildItem c:\docs | ForEach-Object {'Filename: {0} Created: {1}' -f $_.fullname,$_.creationtime}

Display only the Year from a date time value:

PS C:\> "{0:yyyy}" -f (Get-Date)
2018

Display the hours and minutes from a date time value:

PS C:\> "{0:hh}:{0:mm}" -f (Get-Date)
17:52

Reverse the order of display:

PS C:\> "{2} {1,-10} {0:n3}" -f [math]::pi, "world", "hello"
hello world 3.142

Display a number as a percentage:

PS C:\> "{0:p0}" -f 0.5
50%

Display a whole number padded to 5 digits:

PS C:\> "{0:d5}" -f 123
00123

“I skate to where the puck is going to be, not where it has been” ~ Wayne Gretsky

Related PowerShell Cmdlets

PowerShell Operators - Format strings and arrays.
Format-Hex - Displays a file or other input as hexadecimal.
Variables - PowerShell Variables and basic Mathematical operators (+ - = /).
Pipelines - Pass objects down the pipeline.


 
Copyright © 1999-2024 SS64.com
Some rights reserved