I have the following string: "02-06-2018 16:25:28". I need to convert it to a DateTime object. I tried doing this:
[DateTime]::ParseExact('02-06-2018 16:25:28', 'dd-MM-yyyy hh:mm:ss', $null)
But it did not work:
String was not recognized as a valid DateTime.
Is there another function other than [DateTime]::ParseExact that supports parsing the string in this fomat dd-MM-yyyy hh:mm:ss? 
I'm using PowerShell v5.1.
You need HH for a 24-hour clock. hh is for a 12-hour clock, which doesn't recognize a 16th hour. I would also recommend using InvariantCulture instead of $null, as the latter sometimes won't work.
$culture = [Globalization.CultureInfo]::InvariantCulture
[DateTime]::ParseExact('02-06-2018 16:25:28', 'MM-dd-yyyy HH:mm:ss', $culture)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With