' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'D or d', '\'Room #{0:D}\' -f 232', 'Room #232' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'D or d', '\'Invoice No.: {0:D8}\' -f 17', 'Invoice No.: 00000017' );
addFormattingOption( 'Standard Numeric Format Strings', 'E or e', 'Scientific (exponential) notation, with optional precision.' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'E or e', '\'Pi is {0:E}\' -f [Math]::Pi', 'Pi is 3.141593E+000' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'E or e', '\'Pi is {0:E16}\' -f [Math]::Pi', 'Pi is 3.1415926535897931E+000' );
addFormattingOption( 'Standard Numeric Format Strings', 'F or f', 'Fixed point decimal (i.e.: fixed number of decimal places) with optional precision. The default level of precision is two decimal places.' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'F or f', '\'Temperature: {0:F}°C\' -f 18.456', 'Temperature: 18.46°C' );
addFormattingOption( 'Standard Numeric Format Strings', 'G or g', 'Referred to as the "general" format on MSDN. Represents the given number using either scientific or fixed-point notation. PowerShell chooses the most compact notation within the constraints of the specified number of digits. This behaviour is similar to that of a calculator\'s display.' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'G or g', '\'{0:G1}\' -f 12345.67', '1E+04' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'G or g', '\'{0:G2}\' -f 12345.67', '1.2E+04' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'G or g', '\'{0:G3}\' -f 12345.67', '1.23E+04' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'G or g', '\'{0:G4}\' -f 12345.67', '1.235E+04' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'G or g', '\'{0:G5}\' -f 12345.67', '12346' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'G or g', '\'{0:G6}\' -f 12345.67', '12345.7' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'G or g', '\'{0:G7}\' -f 12345.67', '12345.67' );
addFormattingOption( 'Standard Numeric Format Strings', 'N or n', 'Represents the given number in thousand-separated format with optional precision (the default being two decimal places).' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'N or n', '\'File size: {0:N0} bytes(s).\' -f 1.5MB;', 'File size: 1,572,864 bytes(s).' );
addFormattingOption( 'Standard Numeric Format Strings', 'P or p', 'Formats the given number as a percentage with optional precision (the default being two decimal places). The input value is a fraction, for example, 0.5 represents 50%.' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'P or p', '\'Total marks: {0:p}\' -f 0.875;', 'Total marks: 87.50%' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'P or p', '\'Total marks: {0:p0}\' -f 0.875;', 'Total marks: 88%' );
addFormattingOption( 'Standard Numeric Format Strings', 'R or r', 'Round-trip format. Represents the given number with sufficient precision to guarantee that the same number will be yielded if parsed as text.' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'R or r', '\'{0:r}\' -f 12345678901234567.0;', '12345678901234568' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'R or r', '\'{0:r}\' -f 123456789012345678.0;', '1.2345678901234568E+17' );
addFormattingOption( 'Standard Numeric Format Strings', 'X or x', 'Formats the given number as a hexadecimal value with optional fixed-width zero-padding. The case of the resulting hexadecimal characters matches that of the supplied format specifier. \' );
addFormattingOptionExample( 'Standard Numeric Format Strings', 'X or x', '\'Error code 0x{0:X8}\' -f -2147024891', 'Error code 0x80070005' );
addFormattingOption( 'Custom Numeric Format Strings', '0', 'Zero placeholder; explicitly-zero pads and/or specifies decimal precision.' );
addFormattingOptionExample( 'Custom Numeric Format Strings', '0', '\'{0:00000}\' -f 1234', '01234' );
addFormattingOptionExample( 'Custom Numeric Format Strings', '0', '\'{0:0.000}\' -f [Math]::Pi', '3.142' );
addFormattingOptionExample( 'Custom Numeric Format Strings', '0', '\'{0:00.0000}\' -f 1.23', '01.2300' );
addFormattingOption( 'Custom Numeric Format Strings', '#', 'Digit placeholder; explicitly specifies decimal precision.' );
addFormattingOptionExample( 'Custom Numeric Format Strings', '#', '\'{0:####}\' -f 1234.567;', '1235' );
addFormattingOptionExample( 'Custom Numeric Format Strings', '#', '\'{0:####.##}\' -f 1234.567', '1234.57' );
addFormattingOption( 'Custom Numeric Format Strings', '.', 'Decimal point anchor - see usage with 0 and #, above.' );
addFormattingOption( 'Custom Numeric Format Strings', ',', 'Group separator. When a comma appears before the final # or 0 of the integral\, it acts as a group separator (thousand separator).' );
addFormattingOptionExample( 'Custom Numeric Format Strings', ',', '\'{0:#,#}\' -f 1234567;', '1,234,567' );
addFormattingOptionExample( 'Custom Numeric Format Strings', ',', '\'{0:#,#.##}\' -f 1234567.8901', '1,234,567.89' );
addFormattingOption( 'Custom Numeric Format Strings', ',', 'Scaling operator. When a comma appears after the final # or 0 of the integral\, it causes the preceding value to be divided by 1,000. Multiple consecutive commas can be specified to repeat the effect.' );
addFormattingOptionExample( 'Custom Numeric Format Strings', ',', '\'{0:#,}\' -f 1234567', '1235' );
addFormattingOptionExample( 'Custom Numeric Format Strings', ',', '\'{0:#,,}\' -f 1234567', '1' );
addFormattingOption( 'Custom Numeric Format Strings', '%', 'Percentage placeholder. Multiplies the given number by 100 and inserts the percentage symbol. Similar to P, above, but offers more control over placement of the percentage symbol.' );
addFormattingOptionExample( 'Custom Numeric Format Strings', '%', '\'{0:##.00 %}\' -f 0.495;', '49.50 %' );
addFormattingOption( 'Custom Numeric Format Strings', '"text" or \'text\'', 'Literal string. Allows literal text to be placed within a formatted number.' );
addFormattingOptionExample( 'Custom Numeric Format Strings', '"text" or \'text\'', '\'{0:00" "." "00 "USD"}\' -f 90.99', '90 . 99 USD' );
addFormattingOption( 'Custom Numeric Format Strings', '\\<char>', 'Escape character; the proceeding character is treated as a literal character.' );
addFormattingOptionExample( 'Custom Numeric Format Strings', '\\<char>', '\'{0:\\"000\\"}\' -f 123', '"123"' );
addFormattingOption( 'Custom Numeric Format Strings', ';', 'Section separator. Allows different formatting to be applied to positive, negative and zero numbers.' );
addFormattingOptionExample( 'Custom Numeric Format Strings', ';', '\'{0:pos ##.00;neg 00.00;zero ##.##}\' -f 42', 'pos 42.00' );
addFormattingOptionExample( 'Custom Numeric Format Strings', ';', '\'{0:pos ##.00;neg 00.00;zero 00.##}\' -f -39', 'neg 39' );
addFormattingOptionExample( 'Custom Numeric Format Strings', ';', '\'{0:pos ##.00;neg 00.00;zero 00.##}\' -f 0', 'zero' );
addFormattingOption( 'Custom Numeric Format Strings', 'E or e', 'Customisable scientific (exponential) notation. At least one zero must proceed the E, with an optional + controlling whether a + appears with the output exponent.' );
addFormattingOptionExample( 'Custom Numeric Format Strings', 'E or e', '\'{0:#.###E0}\' -f 1PB;', '1.126E15' );
addFormattingOptionExample( 'Custom Numeric Format Strings', 'E or e', '\'{0:#.###E+0}\' -f 1PB;', '1.126E+15' );
addFormattingOptionExample( 'Custom Numeric Format Strings', 'E or e', '\'{0:#####e0}\' -f 1PB;', '11259e11' );
addFormattingOption( 'Standard DateTime Format Strings', 'd', 'Renders the given date using the current culture\'s short date format.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 'd', '\'{0:d}\' -f (Get-Date)', '27/01/2016' );
addFormattingOption( 'Standard DateTime Format Strings', 'D', 'Renders the given date using the current culture\'s long date format.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 'D', '\'{0:D}\' -f (Get-Date)', '27 January 2016' );
addFormattingOption( 'Standard DateTime Format Strings', 'f', 'Combines the current culture\'s long date format and short time format.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 'f', '\'{0:D}\' -f (Get-Date)', '27 January 2016 17:15' );
addFormattingOption( 'Standard DateTime Format Strings', 'F', 'Combines the current culture\'s long date format and long time format.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 'F', '\'{0:D}\' -f (Get-Date)', '27 January 2016 17:15:42' );
addFormattingOption( 'Standard DateTime Format Strings', 'g', 'Referred to as the short-time "general" date/time format on MSDN. Combines the current culture\'s short date and short time formats.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 'g', '\'{0:g}\' -f (Get-Date)', '27/01/2016 17:15' );
addFormattingOption( 'Standard DateTime Format Strings', 'G', 'Referred to as the long-time "general" date/time format on MSDN. Combines the current culture\'s short date and long time formats.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 'G', '\'{0:G}\' -f (Get-Date)', '27/01/2016 17:15:42' );
addFormattingOption( 'Standard DateTime Format Strings', 'M or m', 'Returns the day number and long month name.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 'M or m', '\'{0:M}\' -f (Get-Date)', '27 January' );
addFormattingOption( 'Standard DateTime Format Strings', 'O or o', 'Round-trip format. Represents the given date and time in such a way as to guarantee that the same value will be yielded if parsed as text.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 'O or o', '\'{0:O}\' -f (Get-Date)', '2016-01-27T17:15:01.3106911+00:00' );
addFormattingOption( 'Standard DateTime Format Strings', 'R or r', 'Returns the given date and time in a format compliant with section 5 of the IETF\'s RFC 822.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 'R or r', '\'{0:R}\' -f (Get-Date -Date \'27/01/2005 17:15:42\')', 'Thu, 27 Jan 2005 17:15:42 GMT' );
addFormattingOption( 'Standard DateTime Format Strings', 's', 'Sortable date format. Date fields are listed in ever reducing significance, that is, year, month, day, Etc.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 's', '\'{0:s}\' -f (Get-Date)', '2016-01-27T17:15:42' );
addFormattingOption( 'Standard DateTime Format Strings', 't', 'Renders the time component of the given DateTime using the current culture\'s short time format.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 't', '\'{0:t}\' -f (Get-Date)', '17:15' );
addFormattingOption( 'Standard DateTime Format Strings', 'T', 'Renders the time component of the given DateTime using the current culture\'s long time format.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 'T', '\'{0:T}\' -f (Get-Date)', '17:15:42' );
addFormattingOption( 'Standard DateTime Format Strings', 'u', 'Universal, sortable date and time.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 'u', '\'{0:u}\' -f (Get-Date)', '2016-01-27 17:15:42Z' );
addFormattingOption( 'Standard DateTime Format Strings', 'U', 'Universal full date format.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 'U', '\'{0:U}\' -f (Get-Date)', '27 January 2016 17:15:42' );
addFormattingOption( 'Standard DateTime Format Strings', 'y or Y', 'Renders the year and month using the current culture\'s format, e.g.: month/year in en-GB and year/month in en-US.' );
addFormattingOptionExample( 'Standard DateTime Format Strings', 'y or Y', '\'{0:Y}\' -f (Get-Date)', 'January 2016' );
addFormattingOption( 'Custom DateTime Format Strings', 'd', 'Day of month (between 1 and 31).' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'd', '\'{0:"Day:" d}\' -f (Get-Date -Date \'01 June 2016\')', 'Day: 1' );
addFormattingOption( 'Custom DateTime Format Strings', 'dd', 'Day of month (between 1 and 31), zero-padded to two characters.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'dd', '\'{0:dd}\' -f (Get-Date -Date \'01 June 2016\')', '01' );
addFormattingOption( 'Custom DateTime Format Strings', 'ddd', 'Short name of the day of week.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'ddd', '\'{0:ddd}\' -f (Get-Date)', 'Wed' );
addFormattingOption( 'Custom DateTime Format Strings', 'dddd', 'Full name of the day of the week.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'dddd', '\'{0:ddd}\' -f (Get-Date)', 'Wednesday' );
addFormattingOption( 'Custom DateTime Format Strings', '
f ff fff ffff fffff ffffff fffffff
', 'Fractions of a second, with f representing tenths (10-1), ff representing hundredths (10-2), through to fffffff representing ten-millionths (10-7).' );
addFormattingOptionExample( 'Custom DateTime Format Strings', '
', 'Same as f through fffffff, above, except that a value is only returned if the fractional data is non-zero.' );
/*
addFormattingOption( 'Custom DateTime Format Strings', 'ff', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'fff', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'ffff', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'fffff', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'ffffff', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'fffffff', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'F', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'FF', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'FFF', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'FFFF', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'FFFFF', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'FFFFFF', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'FFFFFFF', '' );
*/
addFormattingOption( 'Custom DateTime Format Strings', 'g or gg', 'Referred to as the "era" or "period" format on MSDN.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'g or gg', '\'{0:yyyy g}\' -f (Get-Date)', '2017 A.D.' );
addFormattingOption( 'Custom DateTime Format Strings', 'h', 'Hour (between 1 and 12).' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'h', '\'{0:"Hour:" h}\' -f (Get-Date -Date \'01 June 2016 18:30:00\')', 'Hour: 6' );
addFormattingOption( 'Custom DateTime Format Strings', 'hh', 'Hour (between 1 and 12), zero-padded to two characters.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'hh', '\'{0:"Hour:" hh}\' -f (Get-Date -Date \'01 June 2016 18:30:00\')', 'Hour: 06' );
addFormattingOption( 'Custom DateTime Format Strings', 'HH', 'Hour in 24-hour format (between 00 and 23).' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'HH', '\'{0:"Hour:" HH}\' -f (Get-Date -Date \'01 June 2016 18:30:00\')', 'Hour: 18' );
/*
addFormattingOption( 'Custom DateTime Format Strings', 'h', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'hh', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'HH', '' );
*/
addFormattingOption( 'Custom DateTime Format Strings', 'K', 'Time-zone offset from UTC.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'K', '\'{0:"Time zone is UTC"K}\' -f (Get-Date)', 'Time zone is UTC-05:00' );
/*
addFormattingOption( 'Custom DateTime Format Strings', 'm', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'mm', '' );
*/
addFormattingOption( 'Custom DateTime Format Strings', 'm', 'Minutes (between 0 and 59).' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'm', '\'{0:"Minutes:" m}\' -f (Get-Date -Date \'01 June 2016 18:09:00\')', 'Minutes: 9' );
addFormattingOption( 'Custom DateTime Format Strings', 'mm', 'Minutes (between 0 and 59), zero-padded to two characters.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'mm', '\'{0:"Minutes:" mm}\' -f (Get-Date -Date \'01 June 2016 18:09:00\')', 'Minutes: 09' );
addFormattingOption( 'Custom DateTime Format Strings', 'M', 'Month number (between 1 and 12).' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'M', '\'{0:"Month:" M}\' -f (Get-Date -Date \'01 May 2016\')', 'Month: 5' );
addFormattingOption( 'Custom DateTime Format Strings', 'MM', 'Month number (between 1 and 12), zero-padded to two characters.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'MM', '\'{0:"Month:" MM}\' -f (Get-Date -Date \'01 May 2016\')', 'Month: 05' );
addFormattingOption( 'Custom DateTime Format Strings', 'MMM', 'Short month name.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'MMM', '\'{0:MMM}\' -f (Get-Date)', 'Sep' );
addFormattingOption( 'Custom DateTime Format Strings', 'MMMM', 'Long month name.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'MMMM', '\'{0:MMMM}\' -f (Get-Date)', 'September' );
addFormattingOption( 'Custom DateTime Format Strings', 's', 'Seconds (between 0 and 59).' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 's', '\'{0:"Seconds:" s}\' -f (Get-Date -Date \'01 June 2016 18:21:05\')', 'Seconds: 5' );
addFormattingOption( 'Custom DateTime Format Strings', 'ss', 'Seconds (between 0 and 59), zero-padded to two characters.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'ss', '\'{0:"Seconds:" ss}\' -f (Get-Date -Date \'01 June 2016 18:21:05\')', 'Seconds: 05' );
addFormattingOption( 'Custom DateTime Format Strings', 't', 'Abbreviated AM/PM designation (A or AM and P for PM)., ' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 't', '\'{0:hh:mmt}\' -f (Get-Date -Date \'21:32:01\')', '09:32P' );
addFormattingOption( 'Custom DateTime Format Strings', 'tt', 'AM/PM designation.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'tt', '\'{0:hh:mmtt}\' -f (Get-Date -Date \'21:32:01\')', '09:32PM' );
/*
addFormattingOption( 'Custom DateTime Format Strings', 'y', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'yy', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'yyy', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'yyyy', '' );
addFormattingOption( 'Custom DateTime Format Strings', 'yyyyy', '' );
*/
addFormattingOption( 'Custom DateTime Format Strings', '
y yy yyy yyyy yyyyy yyyyyy Etc.
', 'Renders the year with the number of y characters controlling the number of digits. If the date cannot be represented accurately, extra digits will be added.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', '
y yy yyy yyyy yyyyy yyyyyy Etc.
', '\'{0:Year: y}\' -f (Get-Date) \'{0:Year: yy}\' -f (Get-Date) \'{0:Year: yyy}\' -f (Get-Date) \'{0:Year: yyyy}\' -f (Get-Date) \'{0:Year: yyyyy}\' -f (Get-Date) \'{0:Year: yyyyyy}\' -f (Get-Date) ', 'Year: 17 Year: 17 Year: 2017 Year: 2017 Year: 02017 Year: 002017 ' );
addFormattingOption( 'Custom DateTime Format Strings', 'z', 'Hour offset from UTC.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'z', '\'{0:t}{0:%z}\' -f (Get-Date) \', '17:14-5' );
addFormattingOption( 'Custom DateTime Format Strings', 'zz', 'Hour offset from UTC, zero-padded to two characters.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'zz', '\'{0:t}{0:zz}\' -f (Get-Date)', '17:14-05' );
addFormattingOption( 'Custom DateTime Format Strings', 'zzz', 'Hour and minute offset from UTC.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', 'zzz', '\'{0:t}{0:zzz}\' -f (Get-Date)', '17:14-05:00' );
addFormattingOption( 'Custom DateTime Format Strings', ':', 'Time separator. Accepted as a formatting character if used between time formatting specifications.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', ':', '\'{0:hh:mm:ss}\' -f (Get-Date)', '17:14:52' );
addFormattingOption( 'Custom DateTime Format Strings', '"text" or \'text\'', 'Literal string. Allows literal text to be placed within a formatted date.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', '"text" or \'text\'', '\'{0:mm" minutes past "hh}\' -f (Get-Date)', '14 minutes past 05' );
addFormattingOption( 'Custom DateTime Format Strings', '%', 'Causes the following character to be treated as a custom format specification.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', '%', '\'{0:d}\' -f (Get-Date -Date \'01 June 2016\') \'{0:%d}\' -f (Get-Date -Date \'01 June 2016\')', '01/06/2016 1 ' );
addFormattingOption( 'Custom DateTime Format Strings', '\\<char>', 'Escape character; the proceeding character is treated as a literal character.' );
addFormattingOptionExample( 'Custom DateTime Format Strings', '\\<char>', '\'{0:\ddd\d}\' -f (Get-Date -Date \'01 June 2016\')', 'd01d' );
// http://ss64.com/ps/syntax-f-operator.html
PowerShell's -f formatting operator uses the .NET Framework's string formatting "engine"
to manipulate text, numeric, date and time data. In its simplest form it behaves like the C, C++ and PHP
sprintf function, that is, it allows data to be inserted into a string at given positions with optional
width and alignment specifications. However, the -f operator goes much further, providing numerous
format specifications, which will be described below.
The most basic use of the -f operator is simple positional data insertion. In this form,
place holders are inserted into the format string in the desired position(s). The place holders
take the following form:
{<INDEX>}
...with the <INDEX> value specifying which of the supplied data items to insert, for example:
PS C:\Users\JohnDoe> 'The {0} sat on the {1}.' -f 'cat', 'mat';The cat sat on the mat.
PS C:\Users\JohnDoe>
Data items can be referenced multiple times, and in any order, for example:
PS C:\Users\JohnDoe> "{1} had a little {0}.`r`nIt's fleece was white as snow,`r`nAnd everywhere that {1} went,`r`nThe {0} was sure to go." -f 'lamb', 'Mary';Mary had a little lamb.
It's fleece was white as snow,
And everywhere that Mary went,
The lamb was sure to go.
PS C:\Users\JohnDoe>
Using the -f operator in this way makes string construction code far more readable and maintainable, as the structure of
the data and the data itself are kept separate. This becomes particularly useful when building long
strings with numerous items of data interspersed throughout (for example, dynamically generated SQL queries).
Each item of data inserted into a string can be assigned a width and a left or right alignment directive. The place holders
now take the form:
{<INDEX>, <ALIGNMENT><WIDTH>}
...where:
<ALIGNMENT> is either left blank for right-alignment, or - for left-alignment; and
<WIDTH> is the desired width of the data. It should, however, be noted that
data that is wider than the specified width will cause overflow (that is, the data needs "trimming" beforehand; this can
be achieved using Substring()).
For example:
PS C:\Users\JohnDoe> '>{0,-20}<' -f 'Left aligned.';# Left-align; no overflow.>Left aligned. <
PS C:\Users\JohnDoe> '>{0,20}<' -f 'Right aligned.';# Right-align; no overflow.> Right aligned.<
PS C:\Users\JohnDoe> '>{0,-20}<' -f 'Left aligned, but too wide.';# Left-align; data overflows width.>Left aligned, but too wide.<
PS C:\Users\JohnDoe>
So, that's the basics out of the way. To recap, we've seen that we can insert various data items into a given string
and control the width and alignment of each data item. The next step is to transform the input data using various format
specifications. For a final time, the place holders now take the following form:
{<INDEX>, <ALIGNMENT><WIDTH>:<FORMAT_SPEC>}
The remainder of this section will describe each of the available <FORMAT_SPEC> options.
The following format specifications deal with date and time data in the form of .NET's System.DateTime data type.
PowerShell's custom date and time format specifications give full control over date and time formatting. Some of the
specifications overlap those of the standard date and time format strings, described above. However, they only
take effect if the format string is clearly a custom string, for example, {0:d} will always be parsed as a short date format, whereas
{0:d d} would return two instances of the current day of the month.