How to add string value to Windows registry? - batch-file

I want to add this new registry entry via cmd/batch file to run CMD always as administrator. I know I can add it by navigating to the mentioned path in the command. But I want to add it in one of my batch files. And I think to add string value I need to modify my cmd. I am missing something here :-
reg.exe ADD \HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers /t REG_SZ "C:\\WINDOWS\\system32\\cmd.exe"="RUNASADMIN" /f
Can anyone help me with this please?

The command line to use in a batch file executed by 64-bit cmd.exe on Windows x64 or 32-bit cmd.exe executed on Windows x86 running under elevated environment of a local administrator is:
%SystemRoot%\System32\reg.exe ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /f /t REG_SZ /v "C:\WINDOWS\system32\cmd.exe" /d "RUNASADMIN"
Open a command prompt window and run reg /? and next reg add /? for help on used command REG for adding a string value to Windows registry.

Related

NSIS with admin access fails to create reg key via batch script

I have a NSIS script with RequestExecutionLevel admin set and within this I invoke a .bat script which adds a reg key.
When the batch-file is executed through command-prompt the reg key gets added. But when running the installer, it executes the .bat file but fails to add the reg key.
nsExec::ExecToStack '"$pluginsdir${SETUP_PATH}\UpdateNtpConfiguration.bat" $Ip1'
UpdateNtpConfiguration.bat content
set adds=%1
REM Get the list of ntp servers showing up in System Date & Time->Internet Time dropdown
set "num=0"
for /F %%G in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers ^| findstr "^[^a-z] ^("') do if %%G GTR !num! set "num=%%G"
set /A num=num + 1
REM Add address at the end
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v "%num%" /t REG_SZ /d "%adds%" /f
goto:eof
Just like #Anders said, there are built in functions for that.
https://nsis.sourceforge.io/Docs/Chapter4.html#registry
If an an error happens with this command, it will be a lot easier to debug as it is built-in.
If you're not doing anything else in the batch file, it is better to find out if NSIS has a command already integrated with it....
EDIT:
It also might be that the admin installer is run the bat file without admin privs.
After some debugging found out that NSIS was writing the registry values in 32 bit reg space(HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft). I had to append /reg:64 to make it add to the 64 bit reg space.
Also the reason I had the bat script was to have some logic to look for duplicates before adding the key.

Clear the PendingFileRenameOperations registry value

One of my troubleshooting steps is to clear the PendingFileRenameOperations registry value to avoid rebooting a server.
What I would like to do is clear this through a batch file, I don't want to delete it, just clear it.
It's the following registry value
HKEY_LOCAL_MACHINE/SOFTWARE/Session Manager/PendingFileRenameOperations
The command
%SystemRoot%\System32\reg.exe add "HKLM\System\CurrentControlSet\Control\Session Manager" /v PendingFileRenameOperations /t REG_MULTI_SZ /d "" /f
replaces current value of multi-string value PendingFileRenameOperations with an empty string.
The path you wrote in the question does not exist on my Windows 7 x64 machine.
For details on command reg open a command prompt window and run there first reg /? and second reg add /?
But why clearing this registry value used to delete or replace (usually update) files after reboot before Windows loads drivers and starts processes and applications should avoid rebooting a Windows server is beyond my understanding.
Some software just checks for the key existence and doesn't care if its blank,So For deleting Key,Use My Way :
reg delete "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager" /v PendingFileRenameOperations /f

Manipulate registry settings on a Win CE 7 System with a Batch file

I got a problem with manipulating my registry settings. I use a Panel PC with windows embedded compact version 7 (Win Ce). I want change the display-brightness, which is set on "HKEY_CURRENT_USER\ControlPanel\BackLight", by a BAT file. I use following syntax:
reg add "HKCU\ControlPanel\BackLight" /T REG_DWORD /F /D 150 /V ACBacklightLevel
But when I run the file, the error message : "Cannot execute reg.exe"
I tried the Bat File on my PC with windows 7. It works correctly.
Is there a problem with the command "reg add" on Win CE systems? Did i make another mistake?
Thanks in advance
Frank
edit:
I´m not sure if this is important, but I use in the header of my BAT file the line "REGEDIT4". I read in other forum that it is for WIN98, NT and 4.0. But there isn´t any command fpr windows CE. Do you know what it does?
edit 23.04.2015:
I created a .reg file, which increases the brightness of my HMI. It worked when I import it manually. Is it possible to import it automatically via Batch file or something like that? someone know the syntax of this command or got another idea how to do this?
the correct command line is (you must use the cmd):
REG ADD "HKCU\Control Panel\BackLight" /v "ACBacklightLevel" /t REG_DWORD /d 150 /f
for the next time:
REG ADD "Register key name" /v "key name" /t "type" /d "type value" /f

My Batch File keeps looping, but why?

I have written a batch file from a VB.NET program I'm creating.
When I double click on the file in Windows XP it brings up a Command Prompt and appears to be running over and over again.
My batch file is as follows
REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f
REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename\Command" /ve /t REG_SZ /d "P:\Misc\Rename v2.0\Rename v2.0\bin\Debug\Rename v2.0.exe ""%1""" /f
EXIT
I can't understan what I've done wrong, but if I open a command prompt and run it from there, it runs once.
Any help would be gratly appreciated!
Thanks
In windows, if you have a command line executable with the same name of your bat filename, and the batch file contains this command, the batch file keeps looping.
Example:
Create the file net.bat on your desktop.
In your file write this text: net
Double click the file and it will keep looping.
The cause of this behaviour is the order of execution of the commands. The command you want to execute is in one of the folders in your path. But the batch file is in your current folder so it gets executed first, causing the loop.
make sure:
your script is not named like a build-in command or programm
make sure the scripts your script calls are not named like a build-in command or programm
e.g. if your script is called: reeeeeboooot.bat that calls shutdown -t 10 -r, but in the SAME FOLDER resides a shutdown.cmd
reeeeeboooot.bat will actually call shutdown.cmd INSTEAD of the build-in command.
sometimes the easiest things are the hardest. (pretty often actually :-D)
In windows command line if you want to execute a command or a set of commands then we usually put those commands into a .bat file to execute them at any point of time but we must follow some guidelines before doing so.
The file Name and the command name should not be same else it would loop forever.
There should be no file in that directory having same name as the command name.
In Windows Terminal and DOS, to start a program, you only have to specify the filename without extension (such as .bat, .exe, .cmd, .com). Also, it is case-insensitive.
So if you create a batch file and execute REG, the system will first look in the current directory for something like reg.exe or reg.bat (or another executable with that name). Casing is ignored, so it will include REG.exe. If it doesn't find it, then it will look in the directories specified in the %PATH% environment variable.
In your case, you (assumingly) have an exetable named reg.bat in which you specify that it should call REG. So it will try to call itself, because it will first look in the current directory, in which it will find itself with that name.
The easiest fix is to use the full filename+extension instead. So you can simply change
REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f
to
REG.exe ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f

Bound cmd as default program for JS files

I have the following problem. For some reason (I wanted to try something) I set the command prompt (C:\Windows\System32\cmd.exe) as the default program (right click JS-file, properties, opens with -> change... ).
Now, for some reason, I can't change it back to an other program. The option of doing so in the file options has disappeared. Any way to change it back to normal?
Thanks,
Ruben.
EDIT:
output of reg query hkcr\jsfile\shell /s:
HKEY_CLASSES_ROOT\jsfile\shell
(Default) REG_SZ Open
HKEY_CLASSES_ROOT\jsfile\shell\Edit
HKEY_CLASSES_ROOT\jsfile\shell\Edit\Command
(Default) REG_SZ C:\Windows\System32\Notepad.exe %1
HKEY_CLASSES_ROOT\jsfile\shell\Edit with Adobe Dreamweaver CS5.5
HKEY_CLASSES_ROOT\jsfile\shell\Edit with Adobe Dreamweaver
CS5.5\Command
(Default) REG_SZ "C:\Program Files (x86)\Adobe\Adobe Dreamweaver CS5.5 \Dreamweaver.exe","%1"
HKEY_CLASSES_ROOT\jsfile\shell\Open
HKEY_CLASSES_ROOT\jsfile\shell\Open\Command
(Default) REG_SZ "C:\Program Files (x86)\Adobe\Adobe Dreamweaver CS5.5 \dreamweaver.exe","%1"
HKEY_CLASSES_ROOT\jsfile\shell\Open\ddeexec
(Default) REG_SZ
HKEY_CLASSES_ROOT\jsfile\shell\Open2
(Default) REG_SZ Open &with Command Prompt
MUIVerb REG_SZ #C:\Windows\System32\wshext.dll,-4511
HKEY_CLASSES_ROOT\jsfile\shell\Open2\Command
(Default) REG_SZ C:\Windows\System32\CScript.exe "%1" %*
HKEY_CLASSES_ROOT\jsfile\shell\Print
HKEY_CLASSES_ROOT\jsfile\shell\Print\Command
(Default) REG_SZ C:\Windows\System32\Notepad.exe /p %1
Open a cmd box and use ASSOC and FTYPE.
Output on my Windows 7 system:
C:\Windows\System32>assoc .js
.js=JSFile
C:\Windows\System32>ftype jsfile
jsfile=C:\Windows\System32\WScript.exe "%1" %*
Using ASSOC, you can find out which filetype is associated to .js
Assuming "JSFile", you can use "ftype jsfile" to find the command defined as default for JSFile. "ftype /?" explains how to change the command.

Resources