I came across a problem when trying to convert a string using [datetime]::ParseExact function. I'm using Get-Filemetadata function (see https://gallery.technet.microsoft.com/scriptcenter/Get-FileMetaData-3a7ddea7) to extract datetimes for record, create, modify etc. from files. The error occurs when trying to convert the record-date to datetime (create and modify dates just work fine?!). I'm using Powershell ISE.
To narrow down the cause I copied the output of the record-date to a separate script, so now I have two strings and a pattern for parsing. The problem only occurs when using this copied record-date. If I enter the date manually into the code, the conversion works fine (as expected). They are visually identical, but for the record-date I get the error:
Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
Which is the same error as in my major script..
I saved the script to a file and opened it with Notepad++. It was looking just as in Powershell ISE. I saved a copy using Notepad++ and re-opened that copy using Powershell ISE.
But now the second string shows as $raw = "12.‎08.‎2018 â€â€Ž13:24"
, so I guess that's the problem.
I have already tried to split and re-merge the record-date to match the pattern, but the error remains. So I assume those hidden chars are coming directly from the file meta-data and are not removed during split/merge..
Powershell ISE:
$pattern = 'dd.MM.yyyy HH:mm'
$raw = "28.08.2018 13:24" #manually entered
[DateTime]::ParseExact($raw, $pattern, $null) #output: Dienstag, 28. August 2018 13:24:00
$raw = "12.08.2018 13:24" #copied from powershell output
[DateTime]::ParseExact($raw, $pattern, $null) #output: Exception error..
Copy from Notepad++ re-opened with Powershell ISE:
$pattern = 'dd.MM.yyyy HH:mm'
$raw = "28.08.2018 13:24"
[DateTime]::ParseExact($raw, $pattern, $null)
$raw = "12.‎08.‎2018 â€â€Ž13:24"
[DateTime]::ParseExact($raw, $pattern, $null)
I need to parse the record-date output from the get-filemetadata function to datetime for sorting purposes.
Thanks in advance..