String formating Powershell
Examples on how you can format string in Powershell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | # String Formatting - C# like syntax is supported # In C you'd use: [string]::Format("There are {0} items.", $items) # Powershell shortcut "There are {0} items." -f $items "There are {0} items in the location {1}." -f $items, $loc "There are {0} items in the location {1}. Wow, {0} is a lot of items!" -f $items, $loc # Predefined formats # N - Number "N0 {0:N0} formatted" -f 12345678.119 # N0 12,345,678 formatted "N1 {0:N1} formatted" -f 12345678.119 # N1 12,345,678.1 formatted "N2 {0:N2} formatted" -f 12345678.119 # N2 12,345,678.12 formatted "N2 {0:N9} formatted" -f 12345678.119 # N2 12,345,678.12 formatted "N0 {0:N0} formatted" -f 123.119 # N0 123 formatted "N0 {0,8:N0} formatted" -f 123.119 # N0 123 formatted # C - Currency "C0 {0:C0} formatted" -f 12345678.1234 # C0 $12,345,678 formatted "C1 {0:C1} formatted" -f 12345678.1234 # C1 $12,345,678.1 formatted "C2 {0:C2} formatted" -f 12345678.1234 # C2 $12,345,678.12 formatted # P - Percentage "P0 {0:P0} formatted" -f 0.1234 # P0 12 % formatted "P2 {0:P2} formatted" -f 0.1234 # P2 12.34 % formatted # X - Hex "X0 0x{0:X0} formatted" -f 1234 # X0 0x4D2 formatted "X0 0x{0:X0} formatted" -f 0x4D2 # X0 0x4D2 formatted # D - Decimal "D0 {0:D0} formatted" -f 12345678 # D0 12345678 formatted "D8 {0:D8} formatted" -f 123 # D8 00000123 formatted "D0 {0:D0} formatted" -f 123 # D0 123 formatted "D0 {0,8:D0} formatted" -f 123 # D0 123 formatted # Note, decimal only supports ints. This causes an error: "D0 {0:D0} formatted" -f 123.1 # Custom formatting $items = 1234 "There are {0:#,#0} items." -f $items # There are 1,234 items. "Custom 0, 25 $#,##0.0000 = {0,25:$ #,##0.0000} " -f 123456789.012000005 # Custom 0, 25 $#,##0.0000 = $ 123,456,789.0120 "Custom 0, 25 $#,##0.0000 = {0,25:$ #,##0.00} " -f 123456789.012000005 # Custom 0, 25 $#,##0.0000 = $ 123,456,789.01 "Custom 0, 25 $#,##0.0000 = {0,25:$ #,##0.00} " -f 123456789.012000005 # Custom 0, 25 $#,##0.0000 = $ 123,456,789.01 "Custom 0, 10 #,##0% = {0,10:#,##0%} " -f 0.125 # Custom 0, 10 #,##0% = 13% "Custom 0, 10 #,##0.00% = {0,10:#,##0.00%} " -f 0.125 # Custom 0, 10 #,##0.00% = 12.50% # Custom date formatting. Note MM is Month, mm is minute "Today is {0:M/d/yyyy}. Be well." -f $(Get-Date) # Today is 3/13/2014. Be well. "Today is {0,10:MM/dd/yyyy}. Be well." -f $(Get-Date) # Today is 03/13/2014. Be well. "Today is {0,10:yyyyMMdd}. Be well." -f $(Get-Date) # Today is 20140313. Be well. "Today is {0,10:MM/dd/yyyy hh:mm:ss}. Be well." -f $(Get-Date) # Today is 03/13/2014 12:21:19. Be well. # Calculations can be passed in as the item to be formatted "The 20% tip of a 33.33 dollar bill is {0} dollars" -f (33.33 * 0.20) # The 20% tip of a 33.33 dollar bill is 6.666 dollars "The 20% tip of a 33.33 dollar bill is {0:0.00} dollars" -f (33.33 * 0.20) # The 20% tip of a 33.33 dollar bill is 6.67 dollars |
Example is copied from PluralSight