Running a windows reg add command in non-interactive mode - windows

I'm trying to write a small batch script that is supposed to change some registry entries.
Now to do that from the command line one would use the reg add command. And when the specified registry key already exists it asks to overwrite the value.
For example reg add "HKCU\Control Panel\Colors" /v Background /t REG_SZ /d "120 0 0" yields Value Background exists, overwrite(Yes/No)? and only if I press y the command completes.
It does the same when the command is issued from a batch script. Since I would like the process to be automated and no further user input to be required I would like to remove the confirmation request. So is it possible to run this command non-interactively?

You can either use the /F flag as mentioned by Compo in the comments, or you can use a .reg file and start it in silent mode:
start regedit /s "C:\path\to\regfile.reg"

Related

Create alias for a specific command in cmd

Is there anyway to create an alias to cd PATH command in cmd?
For example, instead of typing cd C:\John\Pictures I'd like to type just pictures in cmd and press Enter and it should simply take me to C:\John\Pictures.
Is that possible and how?
Here is an alternative for Windows 10:
1. Create a file called init.cmd and save it to your user folder
C:\Users\YourName\init.cmd
#echo off
doskey c=cls
doskey d=cd %USERPROFILE%\Desktop
doskey e=explorer $*
doskey jp=cd C:\John\Pictures
doskey l=dir /a $*
2. Register it to be applied automatically whenever cmd.exe is executed
In the Command Prompt, run:
reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_EXPAND_SZ /d "%"USERPROFILE"%\init.cmd" /f
3. Restart the Command Prompt
Now close/open the Command Prompt and the aliases will be available.
To unregister, run:
reg delete "HKCU\Software\Microsoft\Command Processor" /v AutoRun
You will need to use the doskey command which creates aliases. For example:
doskey note = "C:\Windows\System32\notepad.exe"
note
creates a macro to open Notepad, then calls it. The macro (note in the above example) must be valid (e.g. no spaces are allowed).
You can also use parameters:
doskey note = "C:\Windows\System32\notepad.exe" $1
note "C:\Users\....\textfile.txt"
By default, doskey macros are only saved for the current session. You can work around this limitation in two ways:
Save the macros in a text file, then load them each time you need them:
A command like:
doskey /macros > %TEMP%\doskey-macros.txt
Will save all the current macro definitions into a text file.
Then to restore all the macros at a later date:
doskey /macrofile=%TEMP%\doskey-macros.txt
After saving the macros in a text file as shown above, instead of loading them every time, run:
reg add "HKCU\Software\Microsoft\Command Processor" /v Autorun /d "doskey /macrofile=\"%TEMP%\doskey-macros.txt\"" /f
so that the macros are set each time you open the cmd.
See this SuperUser answer for more information.
Note: You cannot use command aliases in a batch file.

Trying to remove Startup reg key

I want to run a batch to remove a reg key from the start up.
I created a test key and I'm running the command below
Reg Delete "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run\Test" /F
But when I run the batch as Administrator I get the following error
"ERROR: The system was unable to find the specified registry key or value.
The key does exist in the reg but it will not appear in the command prompt.
Can any see what is going wrong here? or what command do I need to do to remove this reg key?
Replace HKEY_LOCAL_MACHINE with HKLM
Type reg /? at the command line for proper syntax.

Windows batch script reg add crashing

I am making a batch script and I am having trouble with this line
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" /v AUOptions /t REG_DWORD /d 3 /f
When that line runs, the command prompt window closes suddenly. This line is supposed to turn on automatic updates. Thanks for your help.
The command you've shown should work okay, at least in the meaning of any syntas error.
The problem, as per you described in the comments, maybe is that you are not taking into account that when a script has finished, the window has any reason to still open. Or in other words, when all the sentences of you're script are done, the window closes "suddenlly" (the program ends execution).
To pause the batch execution, just use the PAUSE command as follows:
#Echo Off
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" /v AUOptions /t REG_DWORD /d 3 /f
PAUSE
Exit /B 0

How to run a command on command prompt startup in Windows

EDIT
If you want to perform any task at computer startup or based on an
event this is very helpful
http://answers.microsoft.com/en-us/windows/forum/windows_7-performance/how-to-schedule-computer-to-shut-down-at-a-certain/800ed207-f630-480d-8c92-dff2313c193b
Back to the question
I have two questions:
I want some specific commands to be executed when I start command prompt.
e.g. cls to clear my command prompt.
I want to execute some commands in a batch file and wait for the user to enter new commands (if any).
e.g. A batch file which will take the user to some specified folder and then wait for the user to rename/delete a file from the command prompt.
How can I do it?
If you want a defined set of commands to run every time you start a command prompt, the best way to achieve that would be to specify an init script in the AutoRun registry value. Create it like this (an expandable string value allows you to use environment variables like %USERPROFILE%):
reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun ^
/t REG_EXPAND_SZ /d "%"USERPROFILE"%\init.cmd" /f
Then create a file init.cmd in your profile folder:
#echo off
command_A
command_B
...
cls
To remove these changes, delete the registry key:
reg delete "HKCU\Software\Microsoft\Command Processor" /v AutoRun
Make a shortcut
Go to the properties
The bit where it says: C:\Users\<Your username>\Desktop\cmd.exe, you put: -cmd /K <your command here>
e.g.
C:\Users\Lewis\Desktop\cmd.exe -cmd /K color 1f
This is the way to launch 1 command without having to mess about with the registry.
Run multiple commands
You can also use & (and) operator to execute multiple commands.
Eg.
C:\Users\Lewis\Desktop\cmd.exe -cmd /K color 1f & H: & <your command>
Credits: user6589073
I found my answer: I should use the /K switch, using which I can enter a new command on the opened command prompt.
E.g. cmd /K cls will open a command prompt for me and clear it. (Answer for question 1)
and
cmd /K MyBatchFile.bat will start a command prompt, execute the batch file and stay on the command prompt and will not exit. (Answer for question 2).
First, you need to press Windows Key + R.
In the box that appears, type "regedit" (without the quotes).
The Windows Registry Editor should open.
Now, locate to HKEY_CURRENT_USER/Software/Microsoft/Command Processor.
Once you have clicked on Command Processor on the left side, click Edit on the top bar.
Then go to New > String Value in the Edit menu.
Rename the String Value that appears to Autorun.
Right click on Autorun and select Modify.
Under the "Value Data" area, type in the commands you want to run. You can run multiple by typing && between them.
Expanding a bit, here is an alternative for Windows 10 where multiple aliases can be defined and applied to the Command Prompt upon execution.
Create a file called init.cmd containing aliases on your %USERPROFILE% folder:
init.cmd
#echo off
doskey c=cls
doskey d=cd %USERPROFILE%\Desktop
doskey e=explorer $*
doskey g=git status
doskey l=dir /a $*
Register it to be applied whenever the Command Prompt is executed:
In the Command Prompt, run:
reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_EXPAND_SZ /d "%"USERPROFILE"%\init.cmd" /f
Done
Now the contents of init.cmd will run for executions of cmd.exe namely from:
Taskbar shortcut
WIN+R cmd
By typing cmd in the File Explorer address bar
By running cmd.exe directly from C:\Windows\System32
After registering these settings just remember to close/open:
The Command Prompt so the settings are applied
The File Explorer, if you use to launch the cmd via File Explorer address bar
To unregister it, run:
reg delete "HKCU\Software\Microsoft\Command Processor" /v AutoRun
I have a command to run a python program. I do not want to run this command manually after login, I want this command should run automatically after I logged in to my ubuntu. I am using Ubuntu 16.04.
Here is the command.
sh demo_darknet_yolov3.sh , this shell is placed in this directory littro#littro-System-Product-Name:~/MobileNet-YOLO-master/MobileNet-YOLO-master
Depending on your script, you may want to use the cmd.exe /k <input script> method instead of the registry entry Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\autorun
I have found with the latter, that other programs that launch cmd are affected by the registry entry. For example, I cannot get Visual Studio Native Tools prompt to work properly because my script gets in the way. In my case, the script is a menu with 5 options including to launch various programs manually (I like to minimize my auto-run programs) and set various environment variables (ie., printers, proxy settings, default versions of programs, etc).
If you are doing something static I think either method works fine.
I would have commented on the question or an applicable answer, but I do not have the reputation to comment.

How to Create Registry key using a batfile

Need to Create a Registry Key using bat file.Can I create Reg Key using Command prompt or a bat file.
The main purpose behind this , I want to create envoirment variable using bat file.
You can use the Windows built-in command line tools, either regedit.exe or reg.exe, see:
Regedit
REG Command in Windows XP
Reading NT's Registry with REG.EXE
Yes u can create Registry Key using Batch file
here is an example:
for disabling task manager using .bat file:
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System /f /v DisableTaskMgr /t REG_DWORD /d 1
for enabling task manager:
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\System /v DisableTaskMgr /f
You can take Help by entering reg/? in command prompt for various options.
Enjoy.........
SET variable=string
If you want to create a persistent environment variable (i.e. one that not only applies to the current session) you can use setx. No need to mess around with the registry directly if there is a program to do it for you:
SetX has three ways of working:
Syntax 1:
SETX [/S system [/U [domain\]user [/P [password]]]] var value [/M]
Syntax 2:
SETX [/S system [/U [domain\]user [/P [password]]]] var /K regpath [/M]
Syntax 3:
SETX [/S system [/U [domain\]user [/P [password]]]]
/F file {var {/A x,y | /R x,y string}[/M] | /X} [/D delimiters]
Description:
Creates or modifies environment variables in the user or system
environment. Can set variables based on arguments, regkeys or
file input.

Resources