Solved

Checking Registry Settings using a batch file

Posted on 2008-11-02
26
868 Views
Last Modified: 2012-05-05
Is it possible to check certain Registry Settings from a batch file?

I would like to check whether a users setting is:

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer]
"NoActiveDesktop"=dword:00000001

or

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer]
"NoActiveDesktop"=dword:00000000

It's not a big issue since deactivating Active Desktop when already deactived probably doesn't do any harm.

Still, it would be cool if I could make a batchfile using "if exist" or something to tell the user whether his Activce Desktop is on or off.

Thank you in advance.
0
Comment
Question by:sebastianemborg
  • 16
  • 10
26 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 22864513
Sure you can, but it's an extra call that does nearly the same thing...

You're better off using:

reg add hkcu\software\microsoft\windows\currentversion\policies\explorer /v NoActiveDesktop /t REG_DWORD /d 0

But, you can test it using something like:

for /f "tokens=3" %%a in ('reg query hkcu\software\microsoft\windows\currentversion\policies\explorer /v NoActiveDesktop') do (
  if [%%a]==[1] (
    echo Your Active Desktop is enabled
  ) else (
    echo Your Active Desktop is disabled
  )

Granted, I tested this under Vista...can't test windows 2000 at the moment, but it should give you the right idea - if not, please ask. :^)
0
 

Author Comment

by:sebastianemborg
ID: 22870315
Thank you!

I couldn't make it work.

I turns out that there are no REG-command in Windows 2000. There is a SETREG-command, though, and it may be something to work on.

I will continue - and return here at one point soon

See you!
0
 

Author Comment

by:sebastianemborg
ID: 22870858
SETREG - not really, I think...
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 67

Expert Comment

by:sirbounty
ID: 22870878
Try the reg tool from the W2k resource kit found here: http://www.petri.co.il/download_free_reskit_tools.htm
(directly http://www.dynawell.com/download/reskit/microsoft/win2000/reg.zip)

Or, try out regini from the same source link - I've not used it as much, since reg is now included with xp...but it's worth a shot if the prior fails to work for you.
0
 

Author Comment

by:sebastianemborg
ID: 22870955
Yes :-)   just realized REG.EXE is part of I:\SUPPORT\TOOLS\SETUP on my installation cd-rom. I now installed it - it's there - and I will work on.

Thanx again
0
 

Author Comment

by:sebastianemborg
ID: 22871292
Havent tried the regini yet....

The script you suggested seems to fail on w2k. The batch file runs through to the end, displaying none of the ECHO statements.

I tried a "pause" at the end of the file, but it didnt stop.

Tried something different, using the REG command:

@echo off

REG Query HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v "NoActiveDesktop" > nul

IF %ErrorLevel%==1 GOTO Failed
IF %ErrorLevel%==0 GOTO Success

:Failed

start no.txt

goto end

:Success

start yes.txt

goto end

:end

cls
exit


This one work!! I inserted and removed the key severel times and the script launced the appropriate textfiles every time.

However, not only do I need to know if the key is present, I also need to know the value of the key.

This one doesn't work:

REG Query HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v "NoActiveDesktop"=dword:00000001 > nul

It will return "no" no matter if the key is there or not.

I feel I'm close to a solution. Any suggestions, sirbounty?
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22871822
I have like 2 or 3 2k servers left in my domain and it's almost like a needle in a haystack to find them...so sorry I can't test this directly for you...

What does
REG Query HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v "NoActiveDesktop"

produce? (redirect it to a file if you need to).

0
 

Author Comment

by:sebastianemborg
ID: 22871837
Oh, I'm fooling around with the terms: "NoActiveDesktop" is the VALUE, I see now. What I cannot make work is the NUMBERS in the VALUE.

Sorry for my ignorance :-)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22871853
default type is REG_SZ (or string data).
You're looking to apply a DWORD value of 0/1 - so you have to specify the
/t REG_DWORD
parameter (if the 2k version of reg is like the newer one...which I thought it was...)?
0
 

Author Comment

by:sebastianemborg
ID: 22872148
Midnight in Denmark - have to go to sleep now :-)

No success yet. I tried some switches (like /d and /f), which I couldnt make work, but tomorrow I will try again.

Thanx for your patience :-)
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22873217
I'll try to find a w2k server tomorrow... :o)

You might also try reg /? to see if it's a different switch.
0
 

Author Comment

by:sebastianemborg
ID: 22878051
Looked at XP's REG command at work today. Looks identical to w2k's REG command.

Typing: REG QUERY /? at the command prompt gives me three switches: /v , /ve , /s

Must watch Obama & McCain on TV now! But I will not give up on this issue - at least not yet :-)

Have a nice day!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22878108
"Must watch Obama & McCain on TV now" - resist the urge or you'll waste your whole day biting your nails, like me... :o)

You don't have to specify the type on a query
What do you get when you try

REG Query HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v "NoActiveDesktop"

Or you can try this slight variation from above...doesn't search the specific value (in case it doesn't exist) - but searches the results of that key for it...
for /f "tokens=3" %%a in ('reg query hkcu\software\microsoft\windows\currentversion\policies\explorer^|find /i "NoActiveDesktop"') do (
  if [%%a]==[0x1] (
    echo Your Active Desktop is enabled
  ) else (
    echo Your Active Desktop is disabled
  )

Open in new window

0
 

Author Comment

by:sebastianemborg
ID: 22878582
Hi again!

If the value is there, activated, I get this:

! REG.EXE Version 2.0

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
    NoActiveDesktop     REG_DWORD       0x1

----------

If the value is there, deactivated, I get this:

! REG.EXE Version 2.0

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer
    NoActiveDesktop     REG_DWORD       0x0

----------

If the value is NOT there, I get this:

! REG.EXE Version 2.0

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer

Error:  The system was unable to find the specified registry key or value

----------

Clearly, the system sees whether Active Desktop is activeted (0x1 and 0x0). But how to get that into an "if exist" command...

----------


The other sacript (with the token in it) just rushes through. Am I doing something wrong? My batch file looks like this:

@echo off

for /f "tokens=3" %%a in ('reg query hkcu\software\microsoft\windows\currentversion\policies\explorer^|find /i "NoActiveDesktop"') do (
  if [%%a]==[0x1] (
    echo Your Active Desktop is enabled
  ) else (
    echo Your Active Desktop is disabled
  )

Nothing happens on the screen, not even if I insert a "pause".

Yeah, it's quite exciting, the election...
0
 

Author Comment

by:sebastianemborg
ID: 22878621
OOps - the other way around:

Deactivated = 0x1

Activeted = 0x0
0
 

Author Comment

by:sebastianemborg
ID: 22878644
I was fooled by the "NO" in the value.

when the number is 0x1, Active Desktop is off
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22880414
Does this work for you?
for /f "tokens=1-3" %%a in ('reg query hkcu\software\microsoft\windows\currentversion\policies\explorer^|find /i "NoActiveDesktop"') do (
echo Value found is %%a %%b %%c
  if [%%c]==[0x0] (
    echo Your Active Desktop is enabled
  ) else (
    echo Your Active Desktop is disabled
  )

Open in new window

0
 

Author Comment

by:sebastianemborg
ID: 22881628
Nope!

I have no idea why I can't make it work. I read about (FOR /?) at the command prompt and it states severel lines (4 or 5 screens) which looks a lot like the script you provided.

Tomorrow I will run it on a company XP-machine and see what happens.

For now: Obama/Biden/McCain/Sara Palin-time :-)

Thank you again !
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22881650
Ok - I'll look at it again myself.
For now - play with the numbers... :^)
  http://www.realclearpolitics.com/epolls/maps/obama_vs_mccain/
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 250 total points
ID: 22885773
Working for me from a command line..try this batch file:
for /f "tokens=1-3" %%a in ('reg query hkcu\software\microsoft\windows\currentversion\policies\explorer^|find /i "NoActiveDesktop"') do call :process %%a %%b %%c
goto :eof
 
:process
If [%3]==[] goto :eof
set strVal=%1
set strType=%2
set strData=%3
echo. %strVal% value found to be:
If [%strData:~-1%]==[1] echo.   Enabled (Which means Active Desktop is disabled)
If [%strData:~-1%]==[0] echo.   Disabled (Which means Active Desktop is capable of being enabled)

Open in new window

0
 

Author Comment

by:sebastianemborg
ID: 22887407
Got it!

Works like a charm :-)

Thanks a lot....

It's all down to me.... I have been disabling/enabling using LOCAL_MACHINE instead of CURRENT_USER.

Obviously (now!) I cannot check the existence in current_user when I disabled it in local_machine.

BIG FOOL , I am (but I am the kind of fool who learns from his mistake... - hopefully)

Apart from this, i learned this: I must install the TOOL program from the CD-ROM to make the script work. In XP these tools are default, I think.

---

If you are not tired of this business of mine, come back here later, because now I will work with this:

The script tells me whether active desktop is 0 or 1. Nice - and my initial objective.

If the value is not present at all, the script runs out.

Therefore I will try to combine the script that check if the value exist at all with your script. Something like:

---

@echo off

REG Query HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v "NoActiveDesktop" > nul

IF %ErrorLevel%==1 GOTO Failed
IF %ErrorLevel%==0 GOTO Success

:Failed

echo Active Desktop is not activated. The key is not present.

goto end

:Success

**YOUR SCRIPT**

goto end

:end

cls
pause
exit

---

I'll be back!

Terminator

PS. I dare say Obama had a very good day, indeed...
0
 

Author Comment

by:sebastianemborg
ID: 22887464
Mistake on my part again:

Not:
REG Query HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v "NoActiveDesktop" > nul

But:
REG Query HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v "NoActiveDesktop" > nul
0
 

Author Comment

by:sebastianemborg
ID: 22888263
This script (slightly modified from the one above) acomplish(e)s the following:

an echo statement: ActiveDesktop value is not present

an echo statement: ActiveDesktop value is present and deactivated

an echo statement: ActiveDesktop value is present and activated

@echo off

REG Query HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v "NoActiveDesktop" > nul

IF %ErrorLevel%==1 GOTO Failed
IF %ErrorLevel%==0 GOTO Success

:Failed

cls
echo Active Desktop is not deactivated. The key is not present.

goto end

:Success

for /f "tokens=1-3" %%a in ('reg query hkcu\software\microsoft\windows\currentversion\policies\explorer^|find /i "NoActiveDesktop"') do call :process %%a %%b %%c
goto :eof
 
:process
If [%3]==[] goto :eof
set strVal=%1
set strType=%2
set strData=%3
echo. %strVal% value found to be:
If [%strData:~-1%]==[1] echo.   Enabled (Which means Active Desktop is disabled)
If [%strData:~-1%]==[0] echo.   Disabled (Which means Active Desktop is capable of being enabled)

goto end

:end
pause
cls
exit

----

Very good - thanks to sirbounty.

But it is possible to replace the echo statements with some commands, like

start somefile.txt

I will try to see if that can be done.
0
 

Author Comment

by:sebastianemborg
ID: 22888404
It can be done:

@echo off

REG Query HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer /v "NoActiveDesktop" > nul

IF %ErrorLevel%==1 GOTO Failed
IF %ErrorLevel%==0 GOTO Success

:Failed

cls
start no1.txt

goto end

:Success

for /f "tokens=1-3" %%a in ('reg query hkcu\software\microsoft\windows\currentversion\policies\explorer^|find /i "NoActiveDesktop"') do call :process %%a %%b %%c
goto :eof
 
:process
If [%3]==[] goto :eof
set strVal=%1
set strType=%2
set strData=%3
echo. %strVal% value found to be:
If [%strData:~-1%]==[1] start yes.txt
If [%strData:~-1%]==[0] start no2.txt
goto end

:end
pause
cls
exit

---

Only be sure to create three textfiles: no1.txt, no2.txt and yes.txt.

That all, folks!
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 22889042
Sorry - was away the better part of today...looks like you have it all set now?
Taking your last post, I would suggest specifying the full path of no & yes txt files...
0
 

Author Comment

by:sebastianemborg
ID: 22890197
Don't be sorry, you've been a tremendous helper!

In my case the text files and the batch files are together in a html compilation, so no path needed. I can even place REG.EXE in the compilation, so the user won't have to install TOOLS to run the script :-)
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question