How to Create Registry key using a batfile - windows

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.

Related

Running a windows reg add command in non-interactive mode

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"

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.

Check Privilege Level for .exe files

I've an application to deploy on ~300 Windows 7 computers.
I don't have a setup to install this app, just need to copy/paste in program files directory.
In the binaries dir, I've 9 executable files.
For each .exe, I need to check the box : "Run this program as an administrator" for all users. I've the local admin credentials.
My need is the do that in batch or VBScript or Regedit or any language (Python, C/C++, Java, others...) but NOT in AutoIt / AutoHotkey.
Can you help me to find the solution?
Yes, it is possible to do by setting a simple registry key. In this case, use REG.exe's ADD option to set this registry value via a batch file.
Command: REG ADD [ROOT\]RegKey /v ValueName [/t DataType] [/S Separator] [/d Data] [/f]
For current user only:
REG ADD "HKCU\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v << 'FULL (DOUBLE QUOTED) PATH TO YOUR APPLICATION'S EXECUTABLE FILE >> /d "RUNASADMIN"
For all users:
REG ADD "HKLM\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v << 'FULL (DOUBLE QUOTED) PATH TO YOUR APPLICATION'S EXECUTABLE FILE >> /d "RUNASADMIN"
Example (for all users):
REG ADD "HKLM\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "C:\My Program\MyProgram.exe" /d "RUNASADMIN"
If you want to set this for all users, you should run your batch file with administrator privileges.

Setting a system environment variable from a Windows batch file?

Is it possible to set a environment variable at the system level from a command prompt in Windows 7 (or even XP for that matter). I am running from an elevated command prompt.
When I use the set command (set name=value), the environment variable seems to be only valid for the session of the command prompt.
The XP Support Tools (which can be installed from your XP CD) come with a program called setx.exe:
C:\Program Files\Support Tools>setx /?
SETX: This program is used to set values in the environment
of the machine or currently logged on user using one of three modes.
1) Command Line Mode: setx variable value [-m]
Optional Switches:
-m Set value in the Machine environment. Default is User.
...
For more information and example use: SETX -i
I think Windows 7 actually comes with setx as part of a standard install.
Simple example for how to set JAVA_HOME with setx.exe in command line:
setx JAVA_HOME "C:\Program Files (x86)\Java\jdk1.7.0_04"
This will set environment variable "JAVA_HOME" for current user. If you want to set a variable for all users, you have to use option "/m" (or -m, prior to Windows 7).
Here is an example:
setx /m JAVA_HOME "C:\Program Files (x86)\Java\jdk1.7.0_04"
Note: you have to execute this command as Administrator.
Note: Make sure to run the command setx from an command-line Admin window
If you set a variable via SETX, you cannot use this variable or its changes immediately. You have to restart the processes that want to use it.
Use the following sequence to directly set it in the setting process too (works for me perfectly in scripts that do some init stuff after setting global variables):
SET XYZ=test
SETX XYZ test
For XP, I used a (free/donateware) tool called "RAPIDEE" (Rapid Environment Editor), but SETX is definitely sufficient for Win 7 (I did not know about this before).
System variables can be set through CMD and registry
For ex. reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH
All the commonly used CMD codes and system variables are given here: Set Windows system environment variables using CMD.
Open CMD and type Set
You will get all the values of system variable.
Type set java to know the path details of java installed on your window OS.
SetX is the command that you'll need in most of the cases.Though its possible to use REG or REGEDIT
Using registry editing commands you can avoid some of the restrictions of the SetX command - different data types, variables containing = in their name and so on.
#echo off
:: requires admin elevated permissions
::setting system variable
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v MyVar /D MyVal
::expandable variable
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /T REG_EXPAND_SZ /v MyVar /D MyVal
:: does not require admin permissions
::setting user variable
REG ADD "HKEY_CURRENT_USER\Environment" /v =C: /D "C:\\test"
REG is the pure registry client but its possible also to import the data with REGEDIT though it allows using only hard coded values (or generation of a temp files). The example here is a hybrid file that contains both batch code and registry data (should be saved as .bat - mind that in batch ; are ignored as delimiters while they are used as comments in .reg files):
REGEDIT4
; #ECHO OFF
; CLS
; REGEDIT.EXE /S "%~f0"
; EXIT
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
"SystemVariable"="GlobalValue"
[HKEY_CURRENT_USER\Environment]
"UserVariable"="SomeValue"
Just in case you would need to delete a variable, you could use SETENV from Vincent Fatica available at http://barnyard.syr.edu/~vefatica.
Not exactly recent ('98) but still working on Windows 7 x64.

cmd defaults to F: drive

When I open cmd on my laptop it is defaulting to the F: drive. This is troubling me does anyone know how it got that way or how to get it back to where it opens to the C: drive by default?
Use the command
C:
To change to the drive C. It would of course work for any drive letter.
Very minor nit: if you're using Windows 7 you don't need the cmdhere powertoy, it's built in to Explorer.
You just navigate to a directory in Windows Explorer then hold down the shift key and right click. "Open command window here" is one of the selections on the context menu.
When it comes to opening cmd.exe in a specific directory, I just create a shortcut to cmd.exe and then in the shortcut properties I set "Start in:" to the drive/directory I want it to start in.
Using a shortcut allows me to customize the cmd.exe windows depending on what I'm using it for. For normal file editing/viewing I use a 180x60 window and appropriate font, but when I want to read/search log files I have a shortcut that opens a 260x100 window with a smaller font. That way I can view most long log file lines without having to use the horizontal scroll.
http://blog.stevienova.com/2007/04/08/change-your-default-cmd-prompt-path/
Sometimes, your path when you go to start->run, CMD will be something
you don’t want. In active directory or on an NT domain, sometimes your
default home path might be a network drive. This isn’t so good when
you are offline or drop offline after being online. The CMD prompt is
set to a place where you can’t get to.
To change the path, you can edit the registry (at your own risk)
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USERSoftwareMicrosoftCommand Processor] “Autorun”=”c:”
This will change the path to your c: drive.
I believe it defaults to %HOMEDRIVE%\%HOMEPATH% so if you can muck about with those environment variables that might be an option. I can't edit these environment variables on my company's network, so I had to use the AutoRun to change it to something sane.
quick answer: cmd /k c:
long answer to make it "automagical":
http://windowsxp.mvps.org/autoruncmd.htm
In RegEdit.exe I created a String:
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
The value I used for AutoRun was "D:"
On the start screen / menu, type in "cmd", right-click it and select "Open File - Location".
In the opened window, right-Click on "Command Prompt" icon, select "Properties", and edit the "Start In" property to your desired path. I used "C:\" as an example
If you are opening it from a shortcut change the working dir for the shortcut.
In addition to the other answers, there's a nice powertoy for XP called "open command window here." It adds an option to your right-click context menu when you click inside a folder to open a command window using that directory as the starting path.
http://www.microsoft.com/windowsxp/Downloads/powertoys/Xppowertoys.mspx
I ran into a similar issue where cmd would always open up in a particular directory (annoying when running scripts which invoke cmd). The best way to deal with this is to edit your autorun settings. Raymond Chen has a nice article about this here:
http://blogs.msdn.com/oldnewthing/archive/2007/11/21/6447771.aspx
The summary is that when you start a command shell, it checks the autorun registry key, and executes the commands stored there. The registry keys it checks are:
HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
and/or
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun
Some answers already mentioned AutoRun as a solution.
But that can be very dangerous, as the AutoRun entry will be executed for any new cmd.exe instance (only pipes ignore the AutoRun).
A simple expample that fails:
cd /d E:\myPath
FOR /F "delims=" %%Q in ('dir') do echo - %%Q
With AutoRun=C:, this shows the content of the current path of drive C:
You can still use AutoRun, but it should be a batch script, that checks if it was called interactive, by FOR/F or by drag&drop.
#echo off
REM *** To enable this script, call it by <scriptName> --install
setlocal EnableDelayedExpansion
REM *** ALWAYS make a copy of the complete CMDCMDLINE, else you destroy the original!!!
set "_ccl_=!cmdcmdline!"
REM *** The check is necessary to distinguish between a new cmd.exe instance for a user or for a "FOR /F" sub-command
if "!_ccl_:~1,-2!" == "!comspec!" (
REM ***** INTERACTIVE ****
REM *** %1 contains only data, when the script itself was called from the command line
if "%~1" NEQ "" (
goto :direct_call
)
endlocal
doskey /macrofile="%~dp0\cmdMacros.mac"
echo ********************************************************************
echo * AutoRun executed from "%~f0"
echo * Macros loaded from "%~dp0\cmdMacros.mac"
echo ********************************************************************
cd /d C:\myPath
) ELSE (
REM *** Called by a FOR command, by an explorer click or a drag & drop operation
REM *** Handle PROBLEMATIC Drag&Drop content, if necessary
endlocal
)
exit /b
:direct_call
if "%~1" == "--install" (
reg add "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v "AutoRun" /t REG_SZ /d "%~f0"
exit /b
)
if "%~1" == "--show" (
reg query "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun
exit /b
)
if "%~1" == "--remove" (
reg DELETE "HKEY_CURRENT_USER\Software\Microsoft\Command Processor" /v AutoRun /f
)
exit /b

Resources