July 4, 2025

USEFUL-IT

A blog for USEFUL-IT information

[PS] .Net Formatting

Well, I hope I am finding you well after the new year.

I do not need you to tell what great things you can do with powershell, but I really wanted to share this great thing with you.

It is called formatting with .net.

String Formatting Syntax

The format string supported by the format (-f) operator is a string that contains format items. Each format item takes the form of:

{index[,alignment][:formatstring]}

Custom Numeric Format Strings

You may use custom numeric format strings to format numbers in ways not supported by the standard format strings.

Format specifier (Name) Description Example
0 (Zero placeholder) Specifies the precision and width of a number string. Zeroes not matched by digits in the original number are output as zeroes.
PS >"{0:00.0}" -f 4.12341234
04.1
# (Digit placeholder) Specifies the precision and width of a number string. # symbols not matched by digits in the input number are not output.
PS >"{0:##.#}" -f 4.12341234
4.1
. (Decimal point) Determines the location of the decimal separator.
PS >"{0:##.#}" -f 4.12341234
4.1
, (Thousands separator) When placed between a zero or digit placeholder before the decimal point in a formatting string, adds the separator character between number groups.
PS >"{0:#,#.#}" -f 1234.121234
1,234.1
, (Number scaling) When placed before the literal (or implicit) decimal point in a formatting string, divides the input by 1,000. You may apply this format specifier more than once.
PS >"{0:##,,.000}" -f 1048576
1.049
% (Percentage placeholder) Multiplies the input by 100 and inserts the percent sign where shown in the format specifier.
PS >"{0:%##.000}" -f .68
%68.000
E0E+0E-0e0e+0e-0

(Scientific notation)

Displays the input in scientific notation. The number of zeroes that follow the E define the minimum length of the exponent field.
PS >"{0:##.#E000}" -f 2.71828
27.2E-001
'text' "text" (Literal string) Inserts the provided text literally into the output without affecting formatting.
PS >"{0:#.00'##'}" -f 2.71828
2.72##
; (Section separator) Allows for conditional formatting. If your format specifier contains no section separators, then the formatting statement applies to all input. If your format specifier contains one separator (creating two sections), then the first section applies to positive numbers and zero. The second section applies to negative numbers. If your format specifier contains two separators (creating three sections), then the sections apply to positive numbers, negative numbers, and zero.
PS >"{0:POS;NEG;ZERO}"-f -14
NEG
Other (Other character) Inserts the provided text literally into the output without affecting formatting.
PS >"{0:$## Please}"-f 14
$14 Please

Standard Numeric Format Strings

All format specifiers may be followed by a number between 0 and 99 to control the precision of the formatting.

Format specifier (Name) Description Example
C or c (Currency) A currency amount.
PS >"{0:C}" -f 1.23
$1.23
D or d (Decimal) A decimal amount (for integral types). The precision specifier controls the minimum number of digits in the result.
PS >"{0:D4}" -f 2
0002
E or e (Scientific) Scientific (exponential) notation. The precision specifier controls the number of digits past the decimal point.
PS >"{0:E3}" -f [Math]::Pi
3.142E+000
F or f (Fixed-point) Fixed point notation. The precision specifier controls the number of digits past the decimal point.
PS >"{0:E3}" -f [Math]::Pi
3.142
G or g (General) The most compact representation (between fixed-point and scientific) of the number. The precision specifier controls the number of significant digits.
PS >"{0:G3}" -f [Math]::Pi
3.14
PS >"{0:G3}" -f 1mb
1.05E+06
N or n (Number) The human readable form of the number, which includes separators between number groups. The precision specifier controls the number of digits past the decimal point.
PS >"{0:N4}" -f 1mb
1,048,576.0000
P or p (Percent) The number (generally between 0 and 1) represented as a percentage. The precision specifier controls the number of digits past the decimal point.
PS >"{0:P4}" -f 0.67
67.0000 %
R or r (Round-trip) The single or double number formatted with a precision that guarantees the string (when parsed) will result in the original number again.
PS >"{0:R}" -f (1mb/2.0)
524288
PS >"{0:R}" -f (1mb/9.0)
116508.44444444444
X or x (Hexadecimal) The number converted to a string of hexadecimal digits. The case of the specifier controls the case of the resulting hexadecimal digits. The precision specifier controls the minimum number of digits in the resulting string.
PS >"{0:X4}" -f 1324
052C

About The Author