StackBuck
StackBuck

Reputation: 819

Batch File to Check and Change Registry value

I want my organization to use the default "Print Screen" Windows server 2019 have.
I mean, when user press on the PrtScn button it will display and use the default "Snipping Tool".

I mean, to that tool:

enter image description here

that hidden here:

enter image description here

Now, i want to use batch script (will start at user logon) to check if in HKEY_CURRENT_USER\Control Panel\Keyboard
in PrintScreenKeyForSnippingEnabled the value is set to 0

If yes, then it will change the value to 1
If it's already on 1, then it will do nothing.

So i thought about something like that:

reg query HKEY_CURRENT_USER\Control Panel\Keyboard 
if %ERRORLEVEL% EQU 0 goto reg add "HKEY_CURRENT_USER\Control Panel\Keyboard" /v "PrintScreenKeyForSnippingEnabled" /t REG_DWORD /d 1 /f || goto end

But cant get it to work.

Whats wrong with my code?

Upvotes: 2

Views: 3793

Answers (1)

Mofi
Mofi

Reputation: 49084

The command line

reg query HKEY_CURRENT_USER\Control Panel\Keyboard

outputs the error message:

ERROR: Invalid syntax.
Type "REG QUERY /?" for usage.

The reason is the space character in registry key name which requires that the entire key name is enclosed in double quotes.

reg query "HKEY_CURRENT_USER\Control Panel\Keyboard"

But that query just checks the existence of the key HKEY_CURRENT_USER\Control Panel\Keyboard and lists all values of this registry key on existing as by default. It does not check if the registry key on existing has also a value with name PrintScreenKeyForSnippingEnabled which is a double word value and has the value 1.

However, it does not matter in real if the double word value exists already or not and which value it has at the moment on already existing. The goal is in any case that the double word value is present finally in the registry of the current user with value 1. Therefore no extra steps are needed at all, just

%SystemRoot%\System32\reg.exe add "HKEY_CURRENT_USER\Control Panel\Keyboard" /v "PrintScreenKeyForSnippingEnabled" /t REG_DWORD /d 1 /f >nul

This command creates the registry key Control Panel on not already existing and the subkey Keyboard on not already existing and the value PrintScreenKeyForSnippingEnabled as double word value on not already existing with value 1.
If a double word value with name PrintScreenKeyForSnippingEnabled exists already with a different value, the existing value is replaced by value 1.
And if there is already a value with name PrintScreenKeyForSnippingEnabled of a different type like REG_SZ, the existing value of wrong type is deleted by the command and then the value is recreated of type REG_DWORD with value 1.

The command could still fail due to missing write access permissions, but that is very unlikely here because that would require the modification of the permissions for this key by a user or script executed before.

I recommend to open a command prompt, run first reg /? and read the short output help, next run reg query /? and read the longer output help and last run reg add /? and read once again the entire output help.

Upvotes: 1

Related Questions