0

I'm currently configuring a bunch of PCs for work and created a PowerShell script to do most of it for me but I have noticed that the date format resets when I reboot the PC after changing the computer name and can't figure out what is wrong. The script is running as admin and the below changed the format to dd-MMM-yy, but after a reboot it resets to M/D/YYYY.

# Running script as admin:
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
    $arguments = "& '" + $MyInvocation.MyCommand.Definition + "'"
    Start-Process powershell -Verb runAs -ArgumentList $arguments
    break
}

# Set Short date format
Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sShortDate -Value dd-MMM-yy
5
  • Going out on a limb I'd guess that after the reboot you're logging in with a different user. Your code changes the setting for the current user (i.e. the user running the script). To change the value for other users with an existing profile you need to run the code as those users. To change the value for new users you need to change the setting in HKEY_USERS\.DEFAULT\Control Panel\International. Commented Aug 20, 2019 at 9:27
  • Unable to find that path, i'm using a scala based mini PC that I need to configure getting the below error message Cannot find path 'C:\Users\scala\HKEY_USERS\.DEFAULT\Control Panel\International' Commented Aug 20, 2019 at 9:35
  • By default only HKLM,HKCU are defined as Get-PSProvider Registry drives. Add a HKU with New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS and then Set-ItemProperty -Path "HKU:\.Default\Control Panel\International" -Name sShortDate -Value dd-MMM-yy Commented Aug 20, 2019 at 10:01
  • @P4TTL It's a registry path, not a filesystem path. Commented Aug 20, 2019 at 10:20
  • Nope that hasn't worked either just keeps resetting after I restart the PC, I've checked the user accounts and it's not changing so not sure what's happening to cause it to reset to default unless I do it manually by going to the window and changing it myself. Would you know a command that opens up the date format window? Commented Aug 20, 2019 at 11:07

1 Answer 1

1

As addition to Ansgar Wiechers. If it turns out you need to change it for all users on devices, this works for me to adjust registries for all users on a device. All that should be adjusted is the last line. In your case it could be:

function Set-RegistryValueForAllUsers {

    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [hashtable[]]$RegistryInstance
    )
    try {
        New-PSDrive -Name HKU -PSProvider Registry -Root Registry::HKEY_USERS | Out-Null

        $LoggedOnSids = (Get-ChildItem HKU: | where { $_.Name -match 'S-\d-\d+-(\d+-){1,14}\d+$' }).PSChildName
        Write-Verbose "Found $($LoggedOnSids.Count) logged on user SIDs"
        foreach ($sid in $LoggedOnSids) {
            Write-Verbose -Message "Loading the user registry hive for the logged on SID $sid"
            foreach ($instance in $RegistryInstance) {
              ## !!!Maakt bovenliggende map aan mocht deze niet bestaan. Wanneer bovenstaande map al bestaat zal hij deze overnieuw aanmaken en dus alle keys 
              ##  verwijderen!!! Deze variabele moet dus alleen worden beantwoord met "Nee" als er echt geen bovenliggende map is!                
              if ($env:Bovenliggend -like "Nee") {New-Item -Path "HKU:\$sid\$($instance.Path | Split-Path -Parent)" -Name ($instance.Path | Split-Path -Leaf) -Force | Out-Null }
          Set-ItemProperty -Path "HKU:\$sid\$($instance.Path)" -Name $instance.Name -Value $instance.Value -Type $instance.Type -Force
            }
        }


        Write-Verbose "Setting Active Setup registry value to apply to all other users"
        foreach ($instance in $RegistryInstance) {
            $Guid = [guid]::NewGuid().Guid
            $ActiveSetupRegParentPath = 'HKLM:\Software\Microsoft\Active Setup\Installed Components'
            New-Item -Path $ActiveSetupRegParentPath -Name $Guid -Force | Out-Null
            $ActiveSetupRegPath = "HKLM:\Software\Microsoft\Active Setup\Installed Components\$Guid"
            Write-Verbose "Using registry path '$ActiveSetupRegPath'"

            switch ($instance.Type) {
                'String' {
                    $RegValueType = 'REG_SZ'
                }
                'Dword' {
                    $RegValueType = 'REG_DWORD'
                }
                'Binary' {
                    $RegValueType = 'REG_BINARY'
                }
                'ExpandString' {
                    $RegValueType = 'REG_EXPAND_SZ'
                }
                'MultiString' {
                    $RegValueType = 'REG_MULTI_SZ'
                }
                default {
                    throw "Registry type '$($instance.Type)' not recognized"
                }
            }

            $ActiveSetupValue = "reg add `"{0}`" /v {1} /t {2} /d {3} /f" -f "HKCU\$($instance.Path)", $instance.Name, $RegValueType, $instance.Value
            Write-Verbose -Message "Active setup value is '$ActiveSetupValue'"
            Set-ItemProperty -Path $ActiveSetupRegPath -Name '(Default)' -Value 'Active Setup Test' -Force
            Set-ItemProperty -Path $ActiveSetupRegPath -Name 'Version' -Value '1' -Force
            Set-ItemProperty -Path $ActiveSetupRegPath -Name 'StubPath' -Value $ActiveSetupValue -Force
        }
    } catch {
        Write-Warning -Message $_.Exception.Message
    }
}

Set-RegistryValueForAllUsers -RegistryInstance @{'Name' = "sShortDate"; 'Type' = "String"; 'Value' = "dd-MMM-yy"; 'Path' = "\Control Panel\International"}

Basically it goes into the UIDs of all created (including the template) users, goes to their current user registries and adjusts the data there. The first 99% of the code is simply setting what the Set-RegistryValueForAllUsersshould do, the last line is the actual execution.

Sign up to request clarification or add additional context in comments.

3 Comments

This changed the short date but again on reboot it reset to the default :/ if I can't get this to work would there be a way of opening the control panel at the date format window and I can change it manually? as I've got a fair few to sort out today
Just as test. If you go to the registry -> security and then add "everyone" and give it full control. Then change it and then reboot. Also, no policies/antivirus rolling back the change? and yes, manually it would be control panel -> clock and region -> country/regio and then select the option to change notation of date/time/numbers.
I think there is something blocking it built in to the player, for some reason after I restart the player and rerun the script once it’s set up then restart again it accepts the date format and saves it...just a few more clicks that I don’t want to have to do on 400+ PC’s lol

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.