Change Windows settings with coding - windows

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

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

Run PowerShell [System.windows.forms.messagebox] through a Batch file without cmd window showing

I am trying to create an automated process that will clean user data on every login to a laptop. The main parts are finished and I am in the polishing phase. I have two batch files run on startup that cleans user data from the profile being logged into and they both work. My issue comes with the the cmd window that appears behind my dialogue box (See picture).
Researching how to get this cmd box to be invisible lead me to VBS. I did find a solution for that, but now the dialogue box does not show at all. I believe it is probably because the VBS script is making all windows prompts invisible.
Bottom line is, how can I get my dialogue box to appear without the CMD window opening with it?
The VBScript is currently running under the common startup folder here:
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp
Deletion script:
#echo off
::Set color of script
color 0a
::Title
title Loaner data wipe
set userpreserve="Administrator,All Users,Default,Public,barfiej"
::All files and folders within the parent folders below will be deleted.
c:
del /S /F/ Q "C:\Users\%USERNAME%\AppData\Local\Microsoft\Outlook\*"
del /S /F/ Q "C:\Users\%USERNAME%\Contacts\*"
del /S /F/ Q "C:\Users\%USERNAME%\Desktop\*"
del /S /F/ Q "C:\Users\%USERNAME%\Documents\*"
del /S /F/ Q "C:\Users\%USERNAME%\Downloads\*"
del /S /F/ Q "C:\Users\%USERNAME%\Favorites\*"
del /S /F/ Q "C:\Users\%USERNAME%\Links\*"
del /S /F/ Q "C:\Users\%USERNAME%\Music\*"
del /S /F/ Q "C:\Users\%USERNAME%\OneDrive\*"
del /S /F/ Q "C:\Users\%USERNAME%\OneDrive - Six Continents Hotels, Inc\*"
del /S /F/ Q "C:\Users\%USERNAME%\Pictures\*"
del /S /F/ Q "C:\Users\%USERNAME%\Saved Games\*"
del /S /F/ Q "C:\Users\%USERNAME%\Searches\*"
del /S /F/ Q "C:\Users\%USERNAME%\Videos\*"
::Clear credential manager
For /F "tokens=1,2 delims= " %G in ('cmdkey /list ^| findstr Target') do cmdkey /delete %H
Dialogue box cmd:
powershell -Command "Add-Type -AssemblyName System.Windows.Forms; C:\ProgramData\LoanerBatchFile\dialogue_box.ps1;"
dialogue_box.ps1:
Add-Type -AssemblyName System.Windows.Forms
[System.windows.forms.messagebox]::show("Welcome to your loaner computer.
`nPlease keep the follow the following instructions while using the loaner laptop.
`n- Save all documents to OneDrive. Data is set to be removed from the user profile at each logoff
`n- Use Webmail
`n- Please keep the computer clean
`n- Be sure to return loaner when picking up your computer");
VBS script:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\ProgramData\LoanerBatchFile\UserDataDeletion.bat" & Chr(34), 0
WshShell.Run chr(34) & objShell.Run("C:\ProgramData\LoanerBatchFile\dialogue.bat") & Chr(34), 0
Set WshShell = Nothing
You can try the alternative way:
$wsh = New-Object -ComObject Wscript.Shell
[Void]$wsh.PopUp("Message content here")
As a followup to my initial comment above.
Untested, because I've nothing to test this concept on, but, roughly something like...
Add-Type -AssemblyName System.Windows.Forms
& cmd.exe set userpreserve="Administrator,All Users,Default,Public,barfiej"
# All files and folders within the parent folders below will be deleted.
'C:\Users\%USERNAME%\AppData\Local\Microsoft\Outlook\*',
'C:\Users\%USERNAME%\Contacts\*',
'C:\Users\%USERNAME%\Desktop\*',
'C:\Users\%USERNAME%\Documents\*',
'C:\Users\%USERNAME%\Downloads\*',
'C:\Users\%USERNAME%\Favorites\*',
'C:\Users\%USERNAME%\Links\*',
'C:\Users\%USERNAME%\Music\*',
'C:\Users\%USERNAME%\OneDrive\*',
'C:\Users\%USERNAME%\OneDrive - Six Continents Hotels, Inc\*',
'C:\Users\%USERNAME%\Pictures\*',
'C:\Users\%USERNAME%\Saved Games\*',
'C:\Users\%USERNAME%\Searches\*',
'C:\Users\%USERNAME%\Videos\*' |
ForEach { Remove-Item -Path $PSItem -Recurse -Force}
<#
https://learn.microsoft.com/en-us/archive/blogs/rmilne/script-to-clear-credman
#>
& cmd.exe For /F "tokens=1,2 delims= " %G in ('cmdkey /list ^| findstr Target') do cmdkey /delete %H
[System.windows.forms.messagebox]::show(
"Welcome to your loaner computer.
`nPlease keep the follow the following instructions while using the loaner laptop.
`n- Save all documents to OneDrive. Data is set to be removed from the user profile at each logoff
`n- Use Webmail
`n- Please keep the computer clean
`n- Be sure to return loaner when picking up your computer"
)
Again, put this in logon/startup scheduled task assigned to RunOnce, or at logon.

List Separator script

How can I change the List separator from the command line?
Normally I have to edit a field in: Control Panel → Change keyboards or other input methods → Additional settings
I would love to create a VBScript that I click which automatically changes from , to ;, and another one that goes back.
A single script should suffice. The setting is stored in the registry value HKCU\ControlPanel\International\sList and can be toggled with something like this:
Set sh = CreateObject("WScript.Shell")
path = "HKCU\Control Panel\International\sList"
Set separator = CreateObject("Scripting.Dictionary")
separator.Add True , ";"
separator.Add False, ","
sh.RegWrite path, separator(sh.RegRead(path) = ","), "REG_SZ"
An even easier way to do this is via a .bat file
open notepad, and save the below as semi.bat or whatever you want to call it.
REG ADD "HKEY_CURRENT_USER\Control Panel\International" /f /v "sList" /t "REG_SZ" /d ";"
PAUSE
REG ADD "HKEY_CURRENT_USER\Control Panel\International" /f /v "sList" /t "REG_SZ" /d ","
This will allow you to open the .bat file it will change it to what you need then when you are finished it will change it back.

Turn UAC off with python

Does anyone know of a way to turn User Account Control off with python 3.3 for Windows. Possibly editing the registry? I know this is not reccomended, but for my purposes this would be ideal. Please help!
Nevermind. I found the solution! I simply created a function that deletes just in case it already exists (otherwise it won't work) and recreates a registry entry. Here it is:
import win32com.shell.shell as win32shell
def disable_UAC():
command1 = 'reg delete HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA'
win32shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c ' + command1)
command2 = 'reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f'
win32shell.ShellExecuteEx(lpVerb='runas', lpFile='cmd.exe', lpParameters='/c ' + command2)

How to set value in registry via batch file in Windows?

I am going to set a value to windows registry.
I want to set variable shit for StupidMS in registry, but the result is wrong. Following is my code.
set stupidMS=shit
echo %stupidMS%
reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run" /v "StupidMS" /t REG_SZ /d ^%stupidMS^%
I think the problem is ^%stupidMS^%, but I quite have no idea how to correct it.
reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run" /v "StupidMS" /t REG_SZ /d "%stupidMS%"

Startup script to generate uninstall command

I have a text file of the format:
computername1 uninstallkey1
computername2 uninstallkey2
...
computername200 uninstallkey200
I am trying to write a startup script (batch file or powershell?) that generates a msiexec command that looks up and implants the correct key for each computer it executes on e.g.:
msiexec /x install.msi key=uninstallkey
If I have not made anything clear, please ask and any help is much appreciated!
#ECHO OFF
SETLOCAL
FOR /f "tokens=1*" %%i IN (yourtextfilename.txt) DO (
IF /i %%i==%COMPUTERNAME% ECHO MSIEXEC /x install.msi key=%%j
)
This should do as you require - yourtextfilename.txt contains the data, presumably on a shared drive; finds the line where the computername in column 1 is the same as the computername returned by %computername% in the target computer's environment.
(all case-insensitive EXCEPT %%i and %%j which must match and be the same case)
Command simply ECHOed - remove the ECHO keyword after verification to activate.
In PowerShell,
$comp = Import-CSV -Delimiter " " -Path C:\comp.txt -Header computername,uninstallkey
$comp | ForEach-Object {
if ($env:COMPUTERNAME -eq $_.Computername) {
Start-Process -FilePath "msiexec.exe" -ArgumentList "/x install.msi key=$_.uninstallkey"
}
}

Resources