Demonstrate PowerShel Formatting


posted by Thomas Lee
12-25-2008

Downloads: 449
File size: 6.4kB
Views: 2,725

Embed
Demonstrate PowerShel Formatting
  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. PS C:\Users\tfl> .\get' 
  61. Standard Numeric Format Specifiers 
  62. (C) Currency: . . . . . . . . ($123.00) 
  63. (D) Decimal:. . . . . . . . . -123 
  64. (E) Scientific: . . . . . . . -1.234500E+002 
  65. (F) Fixed point:. . . . . . . -123.45 
  66. (G) General:. . . . . . . . . -123 
  67. (default):. .     . . . . . . -123 
  68. (N) Number: . . . . . . . . . -123.00 
  69. (P) Percent:. . . . . . . . . -12,345.00 % 
  70. (R) Round-trip: . . . . . . . -123.45 
  71. (X) Hexadecimal:. . . . . . . FFFFFF85 
  72.  
  73. Date Format 
  74. (d) Short date: . . . . . . . 12/25/2008 
  75. (D) Long date:. . . . . . . . Thursday, December 25, 2008 
  76. (t) Short time: . . . . . . . 2:05 PM 
  77. (T) Long time:. . . . . . . . 2:05:22 PM 
  78. (f) Full date/short time: . . Thursday, December 25, 2008 2:05 PM 
  79. (F) Full date/long time:. . . Thursday, December 25, 2008 2:05:22 PM 
  80. (g) General date/short time:. 12/25/2008 2:05 PM 
  81. (G) General date/long time: . 12/25/2008 2:05:22 PM 
  82.     (default):. . . . . . . . 12/25/2008 2:05:22 PM 
  83. (M) Month:. . . . . . . . . . December 25 
  84. (R) RFC1123:. . . . . . . . . Thu, 25 Dec 2008 14:05:22 GMT 
  85. (s) Sortable: . . . . . . . . 2008-12-25T14:05:22 
  86. (u) Universal sortable: . . . 2008-12-25 14:05:22Z 
  87. (U) Universal full date/time: Thursday, December 25, 2008 2:05:22 PM 
  88. (Y) Year: . . . . . . . . . . December, 2008 
  89.  
  90. Standard Enumeration Format Specifiers 
  91. (G) General:. . . . . . . . . Green 
  92.     (default):. . . . . . . . Green 
  93. (F) Flags:. . . . . . . . . . Green 
  94. (D) Decimal number: . . . . . 79 
  95. (X) Hexadecimal:. . . . . . . 0000004F 
  96. Standard Enumeration Format Specifiers (again) 
  97. (G) General:. . . . . . . . . Gainsboro 
  98.     (default):. . . . . . . . Gainsboro 
  99. (F) Flags:. . . . . . . . . . Gainsboro 
  100. (D) Decimal number: . . . . . 74 
  101. (X) Hexadecimal:. . . . . . . 0000004A 
  102. .EXAMPLE 
  103. PS C:\Users\tfl> Get-Help .\Get-StringFormat2.ps1' 
  104. Left as an exercise for the reader. 
  105. #> 
  106. # 
  107. # 
  108. # 
  109. # Start of script 
  110. # 
  111. #Format a negative integer or floating-point number in various ways. 
  112. "Standard Numeric Format Specifiers" 
  113. "(C) Currency: . . . . . . . . {0:C}" -f -123, -123.45 
  114. "(D) Decimal:. . . . . . . . . {0:D}" -f -123, -123.45 
  115. "(E) Scientific: . . . . . . . {1:E}" -f -123, -123.45 
  116. "(F) Fixed point:. . . . . . . {1:F}" -f -123, -123.45 
  117. "(G) General:. . . . . . . . . {0:G}" -f -123, -123.45 
  118. "(default):. .     . . . . . . {0}  " -f -123, -123.45 
  119. "(N) Number: . . . . . . . . . {0:N}" -f -123, -123.45 
  120. "(P) Percent:. . . . . . . . . {1:P}" -f -123, -123.45 
  121. "(R) Round-trip: . . . . . . . {1:R}" -f -123, -123.45 
  122. "(X) Hexadecimal:. . . . . . . {0:X}" -f -123, -123.45 
  123. ""     
  124. # Format the current date in various ways. 
  125. $today = Get-Date 
  126. "Date Format" 
  127. "(d) Short date: . . . . . . . {0:d}" -f $today 
  128. "(D) Long date:. . . . . . . . {0:D}" -f $today 
  129. "(t) Short time: . . . . . . . {0:t}" -f $today 
  130. "(T) Long time:. . . . . . . . {0:T}" -f $today 
  131. "(f) Full date/short time: . . {0:f}" -f $today 
  132. "(F) Full date/long time:. . . {0:F}" -f $today 
  133. "(g) General date/short time:. {0:g}" -f $today 
  134. "(G) General date/long time: . {0:G}" -f $today 
  135. "    (default):. . . . . . . . {0} "  -f $today 
  136. "(M) Month:. . . . . . . . . . {0:M}" -f $today 
  137. "(R) RFC1123:. . . . . . . . . {0:R}" -f $today 
  138. "(s) Sortable: . . . . . . . . {0:s}" -f $today 
  139. "(u) Universal sortable: . . . {0:u}" -f $today 
  140. "(U) Universal full date/time: {0:U}" -f $today 
  141. "(Y) Year: . . . . . . . . . . {0:Y}" -f $today 
  142. "" 
  143. # Format a Color enumeration value in various ways. 
  144. "Standard Enumeration Format Specifiers" 
  145. "(G) General:. . . . . . . . . {0:G}" -f  [system.Drawing.KnownColor]::Green 
  146. "    (default):. . . . . . . . {0}"   -f  [system.Drawing.KnownColor]::Green 
  147. "(F) Flags:. . . . . . . . . . {0:F} " -f [system.Drawing.KnownColor]::Green 
  148. "(D) Decimal number: . . . . . {0:D} " -f [system.Drawing.KnownColor]::Green 
  149. "(X) Hexadecimal:. . . . . . . {0:X}" -f  [system.Drawing.KnownColor]::Green 
  150. "Standard Enumeration Format Specifiers (again)" 
  151. "(G) General:. . . . . . . . . {0:G}" -f  [system.Drawing.KnownColor]::Gainsboro 
  152. "    (default):. . . . . . . . {0}"   -f  [system.Drawing.KnownColor]::Gainsboro 
  153. "(F) Flags:. . . . . . . . . . {0:F} " -f [system.Drawing.KnownColor]::Gainsboro 
  154. "(D) Decimal number: . . . . . {0:D} " -f [system.Drawing.KnownColor]::Gainsboro 
  155. "(X) Hexadecimal:. . . . . . . {0:X}" -f  [system.Drawing.KnownColor]::Gainsboro 
This script implements an MSDN sample on .NET Formatting using PowerShell.

Comments

Thomas Lee wrote re: Demonstrate PowerShel Formatting
on 12-27-2008 10:06 AM

Sure would be nice to be able to fix the typo in the header.

Any chance of an edit feature?

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.