Category filter
Script to change date format on Windows
On Windows devices, there are several methods to customize the date format, including running a PowerShell script on the device. With Hexnode’s Execute Custom Script action, you can remotely push a custom script to change the date format on your Windows endpoints.
PowerShell script to set a date format
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
$dateFormat = 'MM/dd/yyyy' Write-Host "Starting script execution..." try{ $status = New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS Function Set-DateFormat($sid) { if(Test-Path "HKU:\${sid}") { Write-Host "updating date format for user:",$sid Set-ItemProperty -path "HKU:\${sid}\Control Panel\International\" -name 'sShortDate' -value $dateFormat } } $userDetails=Get-wmiobject win32_useraccount | where-object{$_.status -eq 'ok'} foreach($user in $userDetails){ $sid=$user.SID Set-DateFormat($sid) } } catch { Write-Host "Error occured while running script -> ",$_.Exception.Message } Write-Host "Script execution completed successfully!" |
Executing the above script will set the date format on the device to MM/dd/yyyy.
There are several options to choose from when deciding on a date format. The example script provided above sets the date on the device in the Short date format. If you wish to set a Long date format, replace the ‘sShortDate’ key with ‘sLongDate’ in the script and provide a suitable value for the $dateFormat variable.
To specify a date format, you must enter the codes representing each part of the date and time in the $dateFormat variable. Refer Microsoft’s documentation to get the format codes for the different date formats.
Examples for date formats:
Format | Date |
---|---|
D/M/yy (Short date) | 5/7/22 |
dddd, MMMM d, yyyy (Long date) | Tuesday, July 5, 2022 |