2

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..

  • 2
    you can clean the `$Raw` content thus >>> `$raw -replace '[^0-9 .:]'` <<< that will replace anything _other than_ digits, the period, and the colon - with nothing at all. – Lee_Dailey Aug 06 '19 at 18:59
  • 2
    It sounds like this may help: https://stackoverflow.com/a/52597482/45375. The hidden control characters presumably surface as sequences such as `‎` due to an encoding mismatch: Windows PowerShell expects ANSI-encoded files, whereas Notepad++ may be creating UTF-8 files _without BOM_, which Windows PowerShell / the ISE then misinterpret as ANSI (note that PowerShell _Core_ defaults to _UTF-8_ and creates BOM-less UTF8 files by default). – mklement0 Aug 06 '19 at 19:00
  • Don't use out-file, or ">" or especially ">>". They encode in unicode. And ">>" or "out-file -append" will append in unicode, even if the file is ascii. – js2010 Aug 06 '19 at 19:10
  • 1
    the link provided by mklement0 actually points to the solution...adding -replace '\p{Cf}' fixed the problem and the date now converts correctly to datetime. thanks! – user3212714 Aug 06 '19 at 19:42

0 Answers0