# 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](https://www.hexnode.com/mobile-device-management/help/executing-custom-scripts-for-windows/) action, you can remotely push a custom script to change the date format on your Windows endpoints.

 Disclaimer:The Sample Scripts provided below are adapted from third-party Open-Source sites.

 

PowerShell script to set a date format
--------------------------------------

PowerShell script to set a date format

$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!" 

   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](https://docs.microsoft.com/en-us/system-center/orchestrator/standard-activities/format-date-time?view=sc-orch-2022) to get the format codes for the different date formats.

### Examples for date formats:

**Format****Date**D/M/yy (Short date)5/7/22dddd, MMMM d, yyyy (Long date)Tuesday, July 5, 2022  Notes:- The specified date format is applied only to the user accounts logged in while executing the script.
- It is recommended to manually validate the script execution on a system before executing the action in bulk.
- Hexnode will not be responsible for any damage/loss to the system on the behavior of the script.