Home > database >  Powershell Registry String to Workable Date
Powershell Registry String to Workable Date

Time:12-14

Cannot convert string format to date using parse exact.

Registry contains a key 2022-10-18T12:40:25

I need to convert this string to a date field in order to count number of days since (compared to today).


$startdate = Get-ItemProperty -Path 'HKLM:SOFTWARE\Adobe\Acrobat Distiller\DC\Installer\' | select-object 'Dummy'


[datetime]::parseexact($startdate,'dd/MM/yyyy HH:mm',$null)

$today = (([datetime]::Now))
$x = New-TimeSpan -Start $startdate -End $today
"$($x.days) $("days have passed since") $($startdate)"
Cannot find an overload for "parseexact" and the argument count: "3".
At line:2 char:1
  [datetime]::parseexact($startdate,'dd/MM/yyyy HH:mm',$null)
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : NotSpecified: (:) [], MethodException
      FullyQualifiedErrorId : MethodCountCouldNotFindBest
 
New-TimeSpan : Cannot bind parameter 'Start'. Cannot convert value "@{dummy=2022-10-18T12:40:25}" to type "System.DateTime". Error: "Cannot convert the "@{dummy=2022-10-18T12:40:25}" value of type 
"Selected.System.Management.Automation.PSCustomObject" to type "System.DateTime"."
At line:5 char:26
  $x = New-TimeSpan -Start $startdate -End $today
                           ~~~~~~~~~~
      CategoryInfo          : InvalidArgument: (:) [New-TimeSpan], ParameterBindingException
      FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.NewTimeSpanCommand
 
 days have passed since @{dummy=2022-10-18T12:40:25}


CodePudding user response:

You get an error because your second argument doesn't match the format of the datetime.

[datetime]::parseexact($startdate,'yyyy-MM-ddTHH:mm:ss',$null)

CodePudding user response:

Your primary problem is that you need to use -ExpandProperty, if you use Select-Object, or you can use simple property access:

  • Select-Object (select) by default returns a [pscustomobject] instance that has the requested properties - even when you're only asking for a single property. To get only that property's value, use the -ExpandProperty parameter instead of the (possibly positionally implied) -Property parameter - see this answer for details and alternatives, notably the ability to simply use (...).SomeProperty

Therefore, the simplest solution is:

$startdate = (
  Get-ItemProperty 'HKLM:SOFTWARE\Adobe\Acrobat Distiller\DC\Installer'
).Dummy

Or, in PSv5 , using Get-ItemPropertyValue:

$startdate =
  Get-ItemPropertyValue 'HKLM:SOFTWARE\Adobe\Acrobat Distiller\DC\Installer' Dummy

As for then parsing the resulting string into a [datetime] instance:

Simply use a [datetime] cast, which recognizes your string format as-is, irrespective of what culture is currently in effect:

[datetime] '2022-10-18T12:40:25'

In essence, PowerShell translates the above into the following ::Parse() call:

[datetime]::Parse('2022-10-18T12:40:25', [cultureinfo]::InvariantCulture)

The verbose alternative is to use an explicit format string with ::ParseExact(), as you've attempted, and as shown in its corrected form in TheMadTechnician's answer.

  • Related