Storing date in registry

This topic contains 5 replies, has 3 voices, and was last updated by  Joel Sallow 3 months, 2 weeks ago.

  • Author
    Posts
  • #97926

    Simon B
    Participant

    If I run the below code it successfully stores today's date as a string in the registry

    $registryPath = "HKCU:\Test"

    $Name = "hasrun"

    $value = Get-Date

    New-ItemProperty -Path $registryPath -Name $name -Value $value -PropertyType string -Force

    however when I try and read it and convert it to a date it converts it to American format

    $regKey = Get-ItemProperty -Path Registry::HKEY_CURRENT_USER\TEST
    $hasrundate = $regKey.hasrun

    $date = [datetime]$hasrundate

    $date
    $hasrundate

    05 April 2018 07:59:50
    04/05/2018 07:59:50

    If I run

    Get-Culture

    LCID Name DisplayName
    —- —- ———–
    2057 en-GB English (United Kingdom)

    My Culture settings are for the uk. Any ideas on how to get the converted date back in the format that they were stored in the registry ??

  • #97929

    Olaf Soyk
    Participant

    Any ideas on how to get the converted date back in the format that they were stored in the registry ??

    You know that you can output a [datetime] object anyway you want, don't you?

    Get-Date -Format 'MM\/dd\/yyyy HH:mm:ss'
  • #97930

    Simon B
    Participant

    Thank Olaf, the issue is we have sites all over the world. The idea behind writing the date to the registry is I have a scheduled task to run on login for an evergreen deployment. I only want this to run on first login (of the day) and not every time a user logs into their device. The scheduled task throw up a message box asking if the user want to upgrade now or defer (they can have 3 deferrals). I have a workaround using (get-date).DayOfYear and storing that then checking if when the scheduled task runs that (get-date).DayOfYear run in the script is greater than the one stored in the registry.

  • #97933

    Olaf Soyk
    Participant

    So you might store the date and time in a format in the registry YOU specify. And in your script you use your own format for the comparison and the logic. If you need to display something to the user you could use whatever format the user is used to.

  • #97941

    Simon B
    Participant

    Thanks, I will go with (get-date).DayOfYear and store that then check in the script when run if (get-date).DayOfYear is greater than the value stored in the registry as we wont be deploying near the end of the year so should not be an issue. I just wish in SCCM that you could get the same defer dialogue box up when deploying an osd as a task sequence as you do when deploying an application i.e. the one where you get the option to defer it
    https://docs.microsoft.com/en-us/sccm/apps/deploy-use/deploy-applications

  • #97959

    Joel Sallow
    Participant

    If you need to compare an existing string formatted date, you can convert it back to datetime:

    # get string date
    $string = Get-Date -Format "yyyyMMdd"
    # get back a comparable datetime object
    $DateTime = Get-Date $String

You must be logged in to reply to this topic.