0v3rl0rd
0v3rl0rd

Reputation: 47

Cannot edit registry via BAT file but command-line works fine

So Strange thing happend today. I was writing some *.bat which edits some registry values. Just for example, let's say that the command is this:

@ECHO ON 
REG add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v "NoPreviewPane" /t REG_DWORD /d "2" /f 
PAUSE

If I type (paste) this to elevated CMD, it works as it should. But if I create a *.bat file with that command and run it: - run as administrator: cmd opens and closes, and nothing happens - run normally: "access denied" - which is ok, because editing from cmd requires admin rights, afaik.

Any ideas what could be wrong?

Upvotes: 2

Views: 2720

Answers (3)

0v3rl0rd
0v3rl0rd

Reputation: 47

So, after a night of research, I came accross this solution. I reset the registry permissions as shown in the link. After that, the problem is gone.

Upvotes: 2

Tersosauros
Tersosauros

Reputation: 923

The HKEY_CURRENT_USER changes when the current user changes (i.e. to Administrator).

You will need to make your script write to the relevant sub-key of HKEY_CURRENT_USERS instead - note the S at the end. (See here for a bit more on this.)

Upvotes: 1

RGuggisberg
RGuggisberg

Reputation: 4750

64 bit OS? Which CMD.exe are you using? YES... there is a 32 bit one and a 64 bit one! You are probably writing to HKEY_CURRENT_USER\Software... in one case and to HKEY_CURRENT_USER\Software\Wow6432Node in the other case. Do REG ADD /? if on a 64 bit OS and you will see the parameter to specify 32/64 bit area of registry.

Otherwise add the line @ECHO ON on the line before your REG ADD and PAUSE on the line after and see if there is an error message.

Upvotes: 0

Related Questions