Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save DBremen/a328c26d4461c747a95fedfb9cc3bc56 to your computer and use it in GitHub Desktop.
Save DBremen/a328c26d4461c747a95fedfb9cc3bc56 to your computer and use it in GitHub Desktop.
PowerShell custom ArgumentTransformation attribute for DateTime example
# define a format that is supported by our attribute
$stringDate = '4/14 4:12PM'
# try it first with the normal datetime type
[datetime]'4/14 4:12PM'
# this threw an error
# define a variable that utilizes the attribute
[datetime][DateTransform()]$dt = $stringDate
# no error
$dt
# now let's add another format string utilizing the parameter of our custom attribute
# note that the parameter accepts an array arguments. The argument(s) need to be always put in parenthesis for an array argument.
[DateTransformAttribute(('xxx dd.MM hh:mm'))]$dt2 = 'xxx 15.12 10:45'
$dt2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment