April 5, 2018 at 7:09 am #97926
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
05 April 2018 07:59:50
If I run
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 ??
April 5, 2018 at 9:23 am #97929
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'
April 5, 2018 at 9:43 am #97930
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.
April 5, 2018 at 9:56 am #97933
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.
April 5, 2018 at 10:02 am #97941
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
April 5, 2018 at 1:45 pm #97959
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.