Guest User

Untitled

a guest
Jul 19th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. function Get-FormatStrings{
  2. <#
  3. .SYNOPSIS
  4. Show common format strings for a given input and the respective outputs
  5. .DESCRIPTION
  6. Show common format strings for a given input and the respective outputs
  7. .PARAMETER ToBeFormatted
  8. The input that should be formatted. This should either be of type [int], [double], or [datetime] otherwise the function returns a warning and exits.
  9. .EXAMPLE
  10. Get-FormatStrings (Get-Date)
  11. #Return common format strings for date objects
  12.  
  13. FormatString Output
  14. ------------ ------
  15. '{0:d}' -f 05/15/2018 15:33:46 5/15/2018
  16. '{0:D}' -f 05/15/2018 15:33:46 Tuesday, May 15, 2018
  17. '{0:f}' -f 05/15/2018 15:33:46 Tuesday, May 15, 2018 3:33 PM
  18. '{0:F}' -f 05/15/2018 15:33:46 Tuesday, May 15, 2018 3:33:46 PM
  19. '{0:g}' -f 05/15/2018 15:33:46 5/15/2018 3:33 PM
  20. '{0:G}' -f 05/15/2018 15:33:46 5/15/2018 3:33:46 PM
  21. ...
  22. #>
  23. [CmdletBinding()]
  24. Param
  25. (
  26. [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
  27. [ValidateScript({
  28. if (-not ($_ -is [int] -or $_ -is [double] -or $_ -is [datetime])) {
  29. throw “‘${_}’ is not of type [int], [double], or [datetime]. Please provide an input of a valid type.”
  30. }
  31. $true
  32. })]
  33. $ToBeFormatted
  34. )
  35. process{
  36. foreach ($item in $ToBeFormatted){
  37. if ($item -is [datetime]){
  38. $formats = 'd;D;f;F;g;G;m;r;s;t;T;u;U;y;dddd;MMMM;dd;yyyy;M/yy;dd-MM-yy;tt;zzz;R;Y' -split ';'
  39. }
  40. elseif ($item -is [int]){
  41. $formats = @'
  42. (#).##
  43. 00.0000
  44. 0.0
  45. 0,0
  46. d1
  47. d2
  48. d3
  49. n1
  50. n2
  51. n3
  52. f
  53. x4
  54. X4
  55. 0%
  56. c
  57. e
  58. 00e+0
  59. g
  60. 0,.
  61. '@ -split "`r`n"
  62. }
  63. elseif ($item -is [double]){
  64. $formats = @'
  65. (#).##
  66. 00.0000
  67. 0.0
  68. 0,0
  69. n1
  70. n2
  71. n3
  72. f
  73. 0%
  74. c
  75. e
  76. 00e+0
  77. g
  78. 0,.
  79. '@ -split "`r`n"
  80. }
  81.  
  82. $output = $formats | ForEach-Object {
  83. [PSCustomObject]@{
  84. FormatString = "{0,-14} {1}" -f "'{0:$_}'", "-f $item"
  85. Output = "{0:$_}" -f $item
  86. }
  87. }
  88. $output | Out-Default
  89. }
  90. }
  91. }
Add Comment
Please, Sign In to add comment