Run reg command in cmd (bat file)? - windows

I'm trying to run this reg code in cmd (bat file), but I couldn't make it work. Where am I doing wrong?
[HKEY_CURRENT_USER\Software\Policies\Microsoft\Internet Explorer\Control Panel]
"HomePage"=dword:00000001
It works if I make it a reg file and double click.
Bat file code (this doesn't work, no errors):
#echo off
reg add "HKCU\Software\Policies\Microsoft\Internet Explorer\Control Panel" /V HomePage /T REG_DWORD /F /D 1

You will probably get an UAC prompt when importing the reg file. If you accept that, you have more rights.
Since you are writing to the 'policies' key, you need to have elevated rights. This part of the registry protected, because it contains settings that are administered by your system administrator.
Alternatively, you may try to run regedit.exe from the command prompt.
regedit.exe /S yourfile.reg
.. should silently import the reg file. See RegEdit Command Line Options Syntax for more command line options.

In command line it's better to use REG tool rather than REGEDIT:
REG IMPORT yourfile.reg
REG is designed for console mode, while REGEDIT is for graphical mode.
This is why running regedit.exe /S yourfile.reg is a bad idea, since you will not be notified if the there's an error, whereas REG Tool will prompt:
> REG IMPORT missing_file.reg
ERROR: Error opening the file. There may be a disk or file system error.
> %windir%\System32\reg.exe /?
REG Operation [Parameter List]
Operation [ QUERY | ADD | DELETE | COPY |
SAVE | LOAD | UNLOAD | RESTORE |
COMPARE | EXPORT | IMPORT | FLAGS ]
Return Code: (Except for REG COMPARE)
0 - Successful
1 - Failed
For help on a specific operation type:
REG Operation /?
Examples:
REG QUERY /?
REG ADD /?
REG DELETE /?
REG COPY /?
REG SAVE /?
REG RESTORE /?
REG LOAD /?
REG UNLOAD /?
REG COMPARE /?
REG EXPORT /?
REG IMPORT /?
REG FLAGS /?

If memory serves correct, the reg add command will NOT create the entire directory path if it does not exist. Meaning that if any of the parent registry keys do not exist then they must be created manually one by one. It is really annoying, I know! Example:
#echo off
reg add "HKCU\Software\Policies"
reg add "HKCU\Software\Policies\Microsoft"
reg add "HKCU\Software\Policies\Microsoft\Internet Explorer"
reg add "HKCU\Software\Policies\Microsoft\Internet Explorer\Control Panel"
reg add "HKCU\Software\Policies\Microsoft\Internet Explorer\Control Panel" /v HomePage /t REG_DWORD /d 1 /f
pause

You could also just create a Group Policy Preference and have it create the reg key for you. (no scripting involved)

Related

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.

Can I Assign a Custom Icon to a File Type

Is there a way i can associate a file type like (.php, .css, .js) to a custom icon, even when the file has been associate with a program.
for example on my computer .php is associated to sublime text, and when i encounter .php on my file system it show ST's icon, is there a way to make it change to a custom icon like the good ol' elephant, but still be associated with ST i.e when i click the file it still open's in ST(== sublime text).
I know how to do it for filetypes assigned to Notepad++.
In Windows 10, Notepad++ keeps hijacking the icons of filetypes that you open (by default) in Notepad++. It was very difficult to break that behaviour, but I finally got it working!
Preparations
1. Registry permissions
Open the Windos registry, and give yourself enough "permissions" for the folders HKEY_CLASSES_ROOT and HKEY_CURRENT_USER. I'm not sure myself what are the minimum settings for the whole procedure to succeed. You've got to play a bit with the settings. Anytime you get an error message in the coming procedure, go back to this step and increase your registry permissions.
2. Make some custom icons
Make some custom icons, for example in the folder C:\ICONS\. I use the following website https://iconverticons.com/online/ to convert 256x256 png-files into ico-files.
3. Install Notepad++
Just get Notepad++. Nothing special here.
4. Open a terminal with admin rights
Type cmd in the windows search bar. Right click on the CMD-icon that appears and choose "Run as administrator" in the popup window.
Registry hacking
Disclaimer: changing your registry can potentially cause damage to your Windows installation. I have written down the procedure below with best intentions, but I deny any responsibility in case something goes wrong.
Note: I DON'T recommend to use this procedure for .bat files. This is a very important file-type for Windows. So I don't know what would happen if you delete its standard registry keys.
The procedure below describes how to assign a custom icon - which you created in C:\ICONS\abcfile.ico - to all *.abc files.
1. Registry hacking, part one
The first registry key you should add is HKEY_CLASSES_ROOT\.abc. In case this key already exists, you might want to delete it. Use the following command to delete the key and all its subkeys:
> REG DELETE HKEY_CLASSES_ROOT\.abc /f
The following figure shows how we will (re)build this registry key and all its subkeys:
You can do it manually or run the following commands:
> REG ADD HKEY_CLASSES_ROOT\.abc /ve /t REG_SZ /d "abc_auto_file"
> REG ADD HKEY_CLASSES_ROOT\.abc /v BrowserFlags /t REG_DWORD /d 0x00000000
> REG ADD HKEY_CLASSES_ROOT\.abc /v EditFlags /t REG_DWORD /d 0x00000000
> REG ADD HKEY_CLASSES_ROOT\.abc /v PerceivedType /t REG_SZ /d "text"
> REG ADD HKEY_CLASSES_ROOT\.abc\DefaultIcon /ve /t REG_SZ /d "C:\ICONS\abcfile.ico,0"
2. Registry hacking, part two
The second registry key you should add is HKEY_CLASSES_ROOT\abc_auto_file. In case this key already exists, you might want to delete it. Use the following command to delete the key and all its subkeys:
> REG DELETE HKEY_CLASSES_ROOT\abc_auto_file /f
The following figure shows how we will (re)build this registry key and all its subkeys:
You can do it manually or run the following commands:
> REG ADD HKEY_CLASSES_ROOT\abc_auto_file /ve /t REG_SZ
> REG ADD HKEY_CLASSES_ROOT\abc_auto_file\DefaultIcon /ve /t REG_SZ /d "C:\ICONS\abcfile.ico"
> REG ADD HKEY_CLASSES_ROOT\abc_auto_file\shell /ve /t REG_SZ
> REG ADD HKEY_CLASSES_ROOT\abc_auto_file\shell\edit /ve /t REG_SZ
> REG ADD HKEY_CLASSES_ROOT\abc_auto_file\shell\edit\command /ve /t
> REG_EXPAND_SZ /d "\"C:\Program Files (x86)\Notepad++\notepad++.exe\" \"^%1\""
> REG ADD HKEY_CLASSES_ROOT\abc_auto_file\shell\open /ve /t REG_SZ
> REG ADD HKEY_CLASSES_ROOT\abc_auto_file\shell\open\command /ve /t
> REG_EXPAND_SZ /d "\"C:\Program Files (x86)\Notepad++\notepad++.exe\" \"^%1\""
3. Registry hacking, part three
The second registry key you should add is HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc. In case this key already exists, you might want to delete it. Use the following command to delete the key and all its subkeys:
> REG DELETE HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc /f
The following figure shows how we will (re)build this registry key and all its subkeys:
You can do it manually or run the following commands:
> REG ADD HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc /ve /t REG_SZ
> REG ADD HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc\OpenWithList /ve /t REG_SZ
> REG ADD HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc\OpenWithProgids /ve /t REG_SZ
> REG ADD HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc\OpenWithProgids /v "C:\Program Files (x86)\Notepad++\notepad++.exe" /t REG_NONE /d 0
> REG ADD HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.abc\OpenWithProgids /v abc_auto_file /t REG_NONE /d 0
Refresh icon cache
The following command should refresh the Windows icon cache:
> %windir%\system32\ie4uinit.exe -show
Assign Notepad++ to the filetype
I've noticed that - sometimes - the changes only take effect after assigning Notepad++ as the default program to open the filetype.
This is how to do that:
Create a file with the given filetype, eg: myFile.abc
Right click on the file, and select "open with" >> "another app"
You should get a popup window like:
Select Notepad++ as the default program
Finish
Normally the icon has changed now! And all files of that particular filetype will have the custom icon. They will open by default in Notepad++.
Perhaps you should again refresh the icon cache:
> %windir%\system32\ie4uinit.exe -show
or restart your system to let it take effect (although restarting wasn't needed for me).

Not sure why REG ADD command line does not run in VS setup post build event

In the post-build Event, I have had the following command line,
REG ADD HKLM\SOFTWARE\Microsoft\Office\12.0\Common\General /v EnableLocalMachineVSTO /t REG_DWORD /d 1
, which adds a DWORD type value under the registry key General.
I don't know what else I am missing here - the command line REG ADD just doesn't seem to work for me. After the installation, that key is still missing. If I run it alone using Command Prompt, it's all good though.
There is some issue with visual studio build events.
Even with admin privilege, it cannot register in HKLM.
instead if u try with HKEY_CURRENT_USER, it works great.
REG ADD HKEY_CURRENT_USER\SOFTWARE\Microsoft\Office\12.0\Common\General /v EnableLocalMachineVSTO /t REG_DWORD /d 1

Is it possible to associate a file type with a .cmd file so it sends that file as an argument when double clicked

i have a dostuff.cmd file that takes one argument and looks something like this:
filepath_to_command_to_run %1
If I type dostuff.cmd FILE into the command prompt it runs fine.
Is it even remotely possible to get it so that I can associate that file type with my dostuff.cmd so that if I double click a file the .cmd will get it as an argument? Right now I've associated the file type with it, but when I double click it runs the stuff in the .cmd without getting the file as an argument.
Right click on the file -> Open With -> Choose Default Program -> Browse to and select your cmd file. ( Choose the checkbox saying always use this for this file type if you want)
Now double clicking the file will open it with the cmd file.
Alternatively, you can set it directly to filepath_to_command_to_run
Once I have achieved the correct way of FILE ASSOCIATION using these cmd commands.
this is just an example:
REG ADD "HKEY_CLASSES_ROOT\Applications\notepad++.exe\shell\open\command" /v # /t REG_SZ /d "\"C:\\Program Files\\Noteepad++\\notepad++.exe\" \"%1\"" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt" /v "Application" /t REG_SZ /d "notepad++.exe" /f
REG ADD "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.txt\OpenWithList" /v "g" /t REG_SZ /d "notepad++.exe" /f
assoc .txt=MyCustomType
ftype MyCustomType="C:\Program Files\Noteepad++\notepad++.exe" "%1"
(it's better to put them in .bat file)

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