Convert-DateStringToDateTimeObject

Hannes Palmquist
Senior Consultant CloudEventually you will stumble upon badly formatted date/time strings that you need to parse. Instead of doing a lot of Split, Trim, Substring and what not you can leverage the DateTime class methods Parse/TryParse/ParseExact. To PS-ify the use of this method I wrote this powershell function that can take any date/time string and convert it to either a DateTime object or a new string format.
So if we have a date/time string like “2018_06_11_11_05_03” we can convert that date time to a date time object by writing:
You can also use the parameter “PatternOut” to set a specific format to return the DateTime object as.
Note that “\” (backslash) and “:” (semi-colon) needs to be escaped.