Home > Back-end >  Change Windows settings with coding
Change Windows settings with coding

Time:01-03

I would like to have a code, that would check current Windows settings for which decimal symbol is currently used, "comma" or "dot". If it is "comma", the code should change it to the "dot" and if it was "dot" - to "comma".

I would like to have this code as a shortcut on the desktop or so, and that it would be run by simply doubleklicking on it. Is there anyone who might help me with that? In my understanding it should be rather easy, but I don't have experience in these tasks, yet. Thanks!!

CodePudding user response:

You can do that in a couple of ways, but I'm guessing you're looking for a reatively easy way of doing it.

Powershell

With Powershell, you can get the current value of the decimal notation by using this:

(Get-ItemProperty -Path "HKCU:\Control Panel\International" -Name sDecimal).sDecimal

And since you're about to change it to something else, you would also need to handle the thousands grouping symbol. Following the above logic, you would do

(Get-ItemProperty -Path "HKCU:\Control Panel\International" -Name sThousand).sThousand

Both of these get the settings for the current user, and changing them would be a change for that user. If you're comfortable with that, you would do the following.

First, open any text editor (Notepad would do, as well), and then paste the following code.

$currentDecimal = (Get-ItemProperty -Path "HKCU:\Control Panel\International" -Name sDecimal).sDecimal # let's get the current decimal separator

# if the current decimal is equal to a dot
if($currentDecimal -eq ".") {
    Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sDecimal -Value ","
    Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sThousand -Value "." # this line will always change the thousands grouping symbol. If you don't want that, omit this line
    $wasDecimalChanged = $true
} elseif($currentDecimal -eq ",") {
    Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sDecimal -Value "."
    Set-ItemProperty -Path "HKCU:\Control Panel\International" -Name sThousand -Value "," # same as in the first if, omit this, if you don't want to change the thousands grouping symbol
    $wasDecimalChanged = $true
} else {
    $wasDecimalChanged = $false
}

if($wasDecimalChanged) {
    write-host("Decimal symbol was changed to "   (Get-ItemProperty -Path "HKCU:\Control Panel\International" -Name sDecimal).sDecimal)
}

exit

You would then save this as a *.ps1 file.

This script may require running with elevated (administrator) privileges. Also, the system you'll be running this script on may require enabling of running Powershell scripts. You can do that in a couple of ways:

  • by changing the registry on that particular system, like this. This will also let you run your script by double clicking on it
  • by manually enabling the running of Powershell scripts, by starting Powershell as an administrator, and running this command: set-executionpolicy remotesigned. After doing that, you would place a script in a directory anywhere in the system. Then you would create a shortcut, and place it on the Desktop / any other location, and by double-clicking, run your script

Please bear in mind that both of these will open up the system in question to possibile exploits and runnings of malicious scripts.


Batch script

If you want to do it through a batch script, it would look something like this.

First, let's see how we can retrieve the current value for the decimal separator.

reg query "HKEY_CURRENT_USER\Control Panel\International" /v sDecimal

This part

reg query "HKEY_CURRENT_USER\Control Panel\International"

let's us know all the keys within that specific registry entry, and that's alright, but we only need the one for the decimal separator. By adding this

/v sDecimal

our command becomes

reg query "HKEY_CURRENT_USER\Control Panel\International" /v sDecimal

and we get what we want. Well, sort of, since the response to our command is:

HKEY_CURRENT_USER\Control Panel\International
    sDecimal    REG_SZ    .

The only thing we need from that response is the last character - the dot (in this case, it might've been a comma). So, to extract the separator, we would need to do something like this (from within the script - running this in command prompt would require some changes).

for /F "tokens=3" %%A in ('reg query "HKEY_CURRENT_USER\Control Panel\International" /v sDecimal') DO (Echo %%A)

This would return just the decimal separator.

The rest of the logic is more or less the same as in the Powershell example, the only thing that differs is the syntax. Putting it all together, we get

@echo off
title "Decimal change"

REM let's get our current decimal symbol, and give its value to a variable
for /F "tokens=3" %%A in ('reg query "HKEY_CURRENT_USER\Control Panel\International" /v sDecimal') DO (SET currentDecimal=%%A)

IF /i "%currentDecimal%"=="," goto changeComma
IF /i "%currentDecimal%"=="." goto changeDecimal

echo Symbol is not a decimal point or a dot! I've changed nothing!
goto commonexit

:changeComma
%SystemRoot%\System32\reg.exe add "HKEY_CURRENT_USER\Control Panel\International" /v sDecimal /t REG_SZ /d "." /f
%SystemRoot%\System32\reg.exe add "HKEY_CURRENT_USER\Control Panel\International" /v sThousand /t REG_SZ /d "," /f
goto commonexit

:changeDecimal
%SystemRoot%\System32\reg.exe add "HKEY_CURRENT_USER\Control Panel\International" /v sDecimal /t REG_SZ /d "," /f
%SystemRoot%\System32\reg.exe add "HKEY_CURRENT_USER\Control Panel\International" /v sThousand /t REG_SZ /d "." /f
goto commonexit

:commonexit
exit

The REG_SZ bit is used because this is the way the value is stored in the registry - if you were to open the Registry editor on your Windows machine, and then navigate to

Computer\HKEY_CURRENT_USER\Control Panel\International

you would see a list of various settings, and all of them would be of the type REG_SZ .

As with the Powershell script, you would c/p this into a Notepad file. Unlike the Powershell script, you would save this one with a *.bat extension.

The notes regarding elevated / admin privileges, and placing a shortcut on the Desktop apply as well.

  • Related