Friday 26 December 2008

Get-Formatstring2.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Demonstrates string formatting using Powershell 
  4. .DESCRIPTION 
  5.     This script formats 2 numbers in a variety of ways, also 
  6.     formats an enum. 
  7.     An adaptation of the C# sample in the MSDN Library. However, 
  8.     this script uses a built-in enum, and displays two. 
  9. .NOTES 
  10.     Author   : Thomas Lee - tfl@psp.co.uk 
  11. .INPUTTYPE 
  12.    This script has no effective parameters. 
  13. .RETURNVALUE 
  14.    This script produces output as shown in the example 
  15. .LINK 
  16.     http://msdn.microsoft.com/en-us/library/fht0f5be.aspx 
  17.     http://www.pshscripts.blogspot.com  
  18. .EXAMPLE 
  19. PS C:\foo> . 'Get-StringFormat2.ps1' 
  20. (C) Currency: . . . . . . . . ($123.00) 
  21. (D) Decimal:. . . . . . . . . -123 
  22. (E) Scientific: . . . . . . . -1.234500E+002 
  23. (F) Fixed point:. . . . . . . -123.45 
  24. (G) General:. . . . . . . . . -123 
  25. (default):. .     . . . . . . -123 
  26. (N) Number: . . . . . . . . . -123.00 
  27. (P) Percent:. . . . . . . . . -12,345.00 % 
  28. (R) Round-trip: . . . . . . . -123.45 
  29. (X) Hexadecimal:. . . . . . . FFFFFF85 
  30.  
  31. Date Format 
  32. (d) Short date: . . . . . . . 12/25/2008 
  33. (D) Long date:. . . . . . . . Thursday, December 25, 2008 
  34. (t) Short time: . . . . . . . 2:12 PM 
  35. (T) Long time:. . . . . . . . 2:12:55 PM 
  36. (f) Full date/short time: . . Thursday, December 25, 2008 2:12 PM 
  37. (F) Full date/long time:. . . Thursday, December 25, 2008 2:12:55 PM 
  38. (g) General date/short time:. 12/25/2008 2:12 PM 
  39. (G) General date/long time: . 12/25/2008 2:12:55 PM 
  40.     (default):. . . . . . . . 12/25/2008 2:12:55 PM 
  41. (M) Month:. . . . . . . . . . December 25 
  42. (R) RFC1123:. . . . . . . . . Thu, 25 Dec 2008 14:12:55 GMT 
  43. (s) Sortable: . . . . . . . . 2008-12-25T14:12:55 
  44. (u) Universal sortable: . . . 2008-12-25 14:12:55Z 
  45. (U) Universal full date/time: Thursday, December 25, 2008 2:12:55 PM 
  46. (Y) Year: . . . . . . . . . . December, 2008 
  47.  
  48. Standard Enumeration Format Specifiers 
  49. (G) General:. . . . . . . . . Green 
  50.     (default):. . . . . . . . Green 
  51. (F) Flags:. . . . . . . . . . Green 
  52. (D) Decimal number: . . . . . 79 
  53. (X) Hexadecimal:. . . . . . . 0000004F 
  54. Standard Enumeration Format Specifiers (again) 
  55. (G) General:. . . . . . . . . Gainsboro 
  56.     (default):. . . . . . . . Gainsboro 
  57. (F) Flags:. . . . . . . . . . Gainsboro 
  58. (D) Decimal number: . . . . . 74 
  59. (X) Hexadecimal:. . . . . . . 0000004A 
  60. .EXAMPLE 
  61. PS C:\Users\tfl> Get-Help .\Get-StringFormat2.ps1' 
  62. Left as an exercise for the reader. 
  63. #> 
  64. ## 
  65. # Start of script 
  66. # #
  67. #Format a negative integer or floating-point number in various ways. 
  68. "Standard Numeric Format Specifiers" 
  69. "(C) Currency: . . . . . . . . {0:C}" -f -123, -123.45 
  70. "(D) Decimal:. . . . . . . . . {0:D}" -f -123, -123.45 
  71. "(E) Scientific: . . . . . . . {1:E}" -f -123, -123.45 
  72. "(F) Fixed point:. . . . . . . {1:F}" -f -123, -123.45 
  73. "(G) General:. . . . . . . . . {0:G}" -f -123, -123.45 
  74. "(default):. .     . . . . . . {0}  " -f -123, -123.45 
  75. "(N) Number: . . . . . . . . . {0:N}" -f -123, -123.45 
  76. "(P) Percent:. . . . . . . . . {1:P}" -f -123, -123.45 
  77. "(R) Round-trip: . . . . . . . {1:R}" -f -123, -123.45 
  78. "(X) Hexadecimal:. . . . . . . {0:X}" -f -123, -123.45 
  79. ""     
  80. # Format the current date in various ways. 
  81. $today = Get-Date 
  82. "Date Format" 
  83. "(d) Short date: . . . . . . . {0:d}" -f $today 
  84. "(D) Long date:. . . . . . . . {0:D}" -f $today 
  85. "(t) Short time: . . . . . . . {0:t}" -f $today 
  86. "(T) Long time:. . . . . . . . {0:T}" -f $today 
  87. "(f) Full date/short time: . . {0:f}" -f $today 
  88. "(F) Full date/long time:. . . {0:F}" -f $today 
  89. "(g) General date/short time:. {0:g}" -f $today 
  90. "(G) General date/long time: . {0:G}" -f $today 
  91. "    (default):. . . . . . . . {0} "  -f $today 
  92. "(M) Month:. . . . . . . . . . {0:M}" -f $today 
  93. "(R) RFC1123:. . . . . . . . . {0:R}" -f $today 
  94. "(s) Sortable: . . . . . . . . {0:s}" -f $today 
  95. "(u) Universal sortable: . . . {0:u}" -f $today 
  96. "(U) Universal full date/time: {0:U}" -f $today 
  97. "(Y) Year: . . . . . . . . . . {0:Y}" -f $today 
  98. "" 
  99. # Format a Color enumeration value in various ways. 
  100. "Standard Enumeration Format Specifiers" 
  101. "(G) General:. . . . . . . . . {0:G}" -f  [system.Drawing.KnownColor]::Green 
  102. "    (default):. . . . . . . . {0}"   -f  [system.Drawing.KnownColor]::Green 
  103. "(F) Flags:. . . . . . . . . . {0:F} " -f [system.Drawing.KnownColor]::Green 
  104. "(D) Decimal number: . . . . . {0:D} " -f [system.Drawing.KnownColor]::Green 
  105. "(X) Hexadecimal:. . . . . . . {0:X}" -f  [system.Drawing.KnownColor]::Green 
  106. "Standard Enumeration Format Specifiers (again)" 
  107. "(G) General:. . . . . . . . . {0:G}" -f  [system.Drawing.KnownColor]::Gainsboro 
  108. "    (default):. . . . . . . . {0}"   -f  [system.Drawing.KnownColor]::Gainsboro 
  109. "(F) Flags:. . . . . . . . . . {0:F} " -f [system.Drawing.KnownColor]::Gainsboro 
  110. "(D) Decimal number: . . . . . {0:D} " -f [system.Drawing.KnownColor]::Gainsboro 
  111. "(X) Hexadecimal:. . . . . . . {0:X}" -f  [system.Drawing.KnownColor]::Gainsboro
  112. # End of Script

No comments: