Link to home
Start Free TrialLog in
Avatar of samiam41
samiam41Flag for United States of America

asked on

reg query command for registry entry

Hi Experts.  I am wanting to include a query command in my login script to detect the presence of an Office 2003 or Office 2007 registry entry on the computer.  I have the reg command but I can't determine how to get the result code 0 (success) or 1 (failure).  What am I missing?

(This login script will be ran on Windows XP pc's)

Looking for HKLM\software\microsoft\office\11.0\outlook or \12.0\outlook

Thanks Experts!
Avatar of michael626
michael626
Flag of Canada image

you could use REG QUERY
reg query \\%computername%\HKLM\software\microsoft\office\12.0\Outlook /ve
Avatar of samiam41

ASKER

Thanks for the quick reply!

I am running:
reg query \\pc\hklm\software\microsoft\office\11.0 /f outlook 

which yields ->

End of search: 1 match(es) found

Open in new window


I guess what I am looking for is how to get errorlevel to equal 0 so the next part will kick on.  Other ideas on how to get script to continue if the value is detected are welcome.  :)
So here is my half-thought out code that works for whatever reason.  I say that because I'm not sure "why" it works.

reg query "hklm\software\microsoft\office\11.0" /f outlook | find /i "1"

if %errorlevel% EQU 1 goto notepad
exit

:notepad
notepad.exe

:end
exit

Open in new window

never mind.  The above code doesn't work.  Getting error levels of 0 and 9009 when I use 11.0 and "error: the system was unable to find the spe....." when I use 12.0
reg query "hklm\software\microsoft\office\11.0" /f outlook | echo %errorlevel%

if %errorlevel% EQU 0 goto notepad
exit

:notepad
notepad.exe

:end
exit

Open in new window

your variable must be cleared if you use it multiple times your last code seems to works
your variable must be cleared if you use it multiple times

Hmm...  Being completely honest with you here, I have no idea what that means.  If there was a level below novice, I'm not even at that level.  :)  I kind of dumb lucked my way through the code above and came out after a lot of trial and error.
Quick search for clearing variable shows to use "set variable=" at the command prompt which I did and then using the command prompt ran the

reg query "hklm\software\microsoft\office\11.0" /f outlook | %errorlevel%

Open in new window

yields a 1 which is what I want (I'm guessing) and will set the if %errorlevel% EQU 1 goto <section1> and will use that section to do an "action".  Is that right?  
Ok.  Hold on.  Before I make this 100x more complicated than it needs to be because of my drastic lack of experience/skillset in this area, would you recommend a different way to find out what value (11.0 or 12.0) of office is in the registry and have the output determine the next action?
what's your goal, i just want to understand if there's an easier way to do that.
Fair enough,

When the user logs in, I would like the login script to query the registry and if the user is on a pc that has Office 2003 (11.0) installed, I need the script to jump down to the Office 2003 section where a set of actions will occur.  If the user has Office 2007 (12.0) installed, the script jumps to that section where a different set of actions occur.

Thoughts?
SOLUTION
Avatar of michael626
michael626
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Michael.  I really need it in a batch file so I can make changes to it (I am not familar with vb).

The script should check the registry for the presence of Office 12.0 and if those keys are detected, the script goes to the Office.2007 portion and modifies that portion of the registry.  If the Office 12.0 keys aren't found, then the script goes to the Office 2003 section and modifies that portion of the registry as defined in the script.

I can fill in the the actions of the registry but can't determine how to use a batch file to script the detection.
 
Two super EE brainiacs gave me this part of a script in a previous question.  Perhaps we can build off this?
2>nul >nul reg query "HKCU\Software\Microsoft\Office\12.0\Outlook\Setup\First-Run" && (
  reg delete "HKCU\Software\Microsoft\Office\12.0\Outlook\Setup\First-Run"
  reg delete ...
) || (
  REM anything to do if the key is already deleted, which usually is - nothing
)

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks DrUltima.  Testing it now!
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh Qlemo,  you can start sh*t like no other.  : )

Since I am way out of my league here, I am going to rely on you, DrUltima and any other EE expert to tell me the best way to achieve the end result which is:

I have 200+ users that I am migrating (in phases) from Office 2003 to Office 2007.  During the migration, I have a set of registry entries that I need to have made depending on the version of Office the user has installed.  I thought the best time for this was when the user logs in and the script is triggered but perhaps this should be done in a GPO?

Thoughts/suggestions/constructive criticisms?
I have no problems with Qlemo writing better code than me. :)  He is much, much better at it.  He isn't starting anything that I probably haven't started with him in other threads, anyway.  ;-)

-DrU
@DrUltima:  Fair enough although I don't know what I am going to do with all of the popcorn I bought to see two titans battle it out...  :)

With all that said, what is the best way to get these registry changes made?  Script, script via GP, reg keys deployed via GP, some other to be mentioned method?  
What tools do you have at your disposal?  Are all your scripts Batch?  Do you use SCCM?  Are you familiar with using GPO to remove and add software?  I would use a script as a last resort.  My preference would be to use SCCM in this scenario.  There are also third party application management tools (CA's Unicenter Desktop and Server Management Software Delivery, for example), but the good ones tend to be pricey.

DrUltima
And, I just noticed after re-reading Q's comment that I accidentally used HKCU rather than HKLM.  Sorry about that.

DrU
I'm using LANguard to deploy the Office 2007 software.  Once the software is deployed, the computer restarts (migration stages happen in the evening).  When the user comes in the next morning, I wanted to make sure that the location for their Outlook prf are set, the first run wizard and RSS update feed is disabled to mention just a couple.  I figured the best way to do all of that was via a login script but have since took a quick second to rethink and consult the tech overlords on the best course.  Since the registry keys they would need to modify are HKCU, they would be able to modify their own (correct)?  
Can LANGuard use an upgrade if the older software is detected, or is it a "one solution" deployment?  Yes, users can generally modify keys under HKCU.

DrUltima
That's a good question.  The best way I can answer that is by saying I am installing Office 2007 software on the computer without uninstalling the existing Office 2003 software that may be installed.  Is that an upgrade?  The LANguard is only deploying the config.xml script that relates to the software package created.  I know you didn't expect a four line answer to a simple question but I can't say for certain if it's a yes or no.

I went ahead and deployed Office 2007 on a virtual pc and since I am confident in your scripting ability, I modified the production login script to include the portion you provided above so I can see how the testing on the virt pc comes out.  :)  Ok, maybe not the production login script but I did have the test user account run this as its login script:

 
:office.2007
REG QUERY "HKCU\Software\Microsoft\Office\12.0" >nul 2>nul && (
     ECHO "Office 2007 Detected" 
     GOTO OFFICE2007
) || (
REG QUERY "HKCU\Software\Microsoft\Office\11.0" >nul 2>nul && (
     ECHO "Office 2003 Detected" 
     GOTO OFFICE2003
)

:OFFICE2007
REM Do your Actions here
reg delete HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Outlook\Setup\First-Run
reg delete HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Outlook\Setup\FirstRun

reg add "HKCU\Software\Microsoft\Office\12.0\Outlook\Setup" /v "ImportPRF" /d "\\w2k3nas01\public\office.2007\prf\cs.outlook07.PRF" /f
reg add "HKCU\Software\Microsoft\Office\12.0\Common\General" /v "ShownOptIn" /t reg_dword /d 0x1 /f
reg add "HKCU\Software\Microsoft\Office\12.0\Outlook\Search" /v "DisableDownloadSearchPrompt" /t reg_dword /d 0x1 /f
reg add "HKCU\Software\Microsoft\Office\12.0\Outlook\Options\RSS" /v "SyncToSysCFL" /t reg_dword /d 0x0 /f
GOTO delete.existing.network.drive.mappings

:OFFICE2003
REM Do your Actions here
set OutlookTimeout=10
reg delete "HKCU\Software\Microsoft\Office\11.0\Outlook\Setup" /v "First-Run" /f
reg delete "HKCU\Software\Microsoft\Office\11.0\Outlook\Setup" /v "FirstRun" /f
reg add "HKCU\Software\Microsoft\Office\11.0\Outlook\Setup" /v "ImportPRF" /d "\\w2k3fs01\login\outlook.PRF" /f
GOTO delete.existing.network.drive.mappings

Open in new window

D@mn I made that complicated.  Office 2007 is installing OVER the Office 2003 products that are listed in the config.xml list.  Since I have Word 2007 listed, it will install Word 2007 over Word 2003 but I don't have Access 2007 included in the list so if the user (for whatever rare reason) has Access 2003, it stays installed and won't get Access 2007.

Sorry for the long answer.
And... Did it work as expected?  BTW, you CAN have all apps in the office suite as both 2003 and 2007 EXCEPT Outlook.  Only one version of it can exist on a computer at a time.

DrUltima
When I run the test script (see below) from the command prompt, I am getting to the "goto office.reg.mod" section and then nothing.  I verified the reg entries are not being made.  I did run

reg add "HKCU\Software\Microsoft\Office\12.0\Outlook\Setup" /v "ImportPRF" /d "\\w2k3nas01\public\office.2007\prf\cs.outlook07.PRF" /f

and the reg entry was created without problem.  The script just seems to not process the "office.reg.mod" section.
REM @echo off
REM --=Login Script created by Aaron L. Shaw=--
REM --=Version 11.03.10=--
REM --=\\w2k3fs01\login\login.bat

if Windows_NT == %OS% goto WINNT

:WINNT
ver

ver | find "XP" /C

if not %errorlevel% EQU 0 goto NOTFOUND
goto office.2007

:NOTFOUND
goto exit

:office.2007
REG QUERY "HKCU\Software\Microsoft\Office\12.0" >nul 2>nul && (
     ECHO "Office 2007 Detected" 
     GOTO OFFICE2007
) || (
REG QUERY "HKCU\Software\Microsoft\Office\11.0" >nul 2>nul && (
     ECHO "Office 2003 Detected" 
     GOTO OFFICE2003
)

:OFFICE2007
REM Do your Actions here
reg delete HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Outlook\Setup\First-Run
reg delete HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Outlook\Setup\FirstRun

reg add "HKCU\Software\Microsoft\Office\12.0\Outlook\Setup" /v "ImportPRF" /d "\\w2k3nas01\public\office.2007\prf\cs.outlook07.PRF" /f
reg add "HKCU\Software\Microsoft\Office\12.0\Common\General" /v "ShownOptIn" /t reg_dword /d 0x1 /f
reg add "HKCU\Software\Microsoft\Office\12.0\Outlook\Search" /v "DisableDownloadSearchPrompt" /t reg_dword /d 0x1 /f
reg add "HKCU\Software\Microsoft\Office\12.0\Outlook\Options\RSS" /v "SyncToSysCFL" /t reg_dword /d 0x0 /f
GOTO delete.existing.network.drive.mappings

:OFFICE2003
REM Do your Actions here
set OutlookTimeout=10
reg delete "HKCU\Software\Microsoft\Office\11.0\Outlook\Setup" /v "First-Run" /f
reg delete "HKCU\Software\Microsoft\Office\11.0\Outlook\Setup" /v "FirstRun" /f
reg add "HKCU\Software\Microsoft\Office\11.0\Outlook\Setup" /v "ImportPRF" /d "\\w2k3fs01\login\outlook.PRF" /f
GOTO delete.existing.network.drive.mappings

:delete.existing.network.drive.mappings
net use i: /delete
net use k: /delete
net use L: /delete
net use m: /delete
net use n: /delete
net use q: /delete
net use s: /delete
net use t: /delete
net use u: /delete

Open in new window

Hold on.  Sorry.  Man this is wearing on me.  Here is the actual copy/paste from the script...
REM @echo off
REM --=Login Script created by Aaron L. Shaw=--
REM --=Version 11.03.10=--
REM --=\\w2k3fs01\login\login.bat

if Windows_NT == %OS% goto WINNT

:WINNT
ver

ver | find "XP" /C

if not %errorlevel% EQU 0 goto NOTFOUND
goto office.reg.mod

:NOTFOUND
goto exit

:office.reg.mod
REG QUERY "HKLM\SOFTWARE\Microsoft\Office\12.0" >nul 2>nul && (
     ECHO "Office 2007 Detected" 
     GOTO OFFICE2007
) || (
REG QUERY "HKLM\Software\Microsoft\Office\11.0" >nul 2>nul && (
     ECHO "Office 2003 Detected" 
     GOTO OFFICE2003
)

:OFFICE2007
REM Do your Actions here

reg add "HKCU\Software\Microsoft\Office\12.0\Outlook\Setup" /v "ImportPRF" /d "\\w2k3nas01\public\office.2007\prf\cs.outlook07.PRF" /f
reg add "HKCU\Software\Microsoft\Office\12.0\Common\General" /v "ShownOptIn" /t reg_dword /d 0x1 /f
reg add "HKCU\Software\Microsoft\Office\12.0\Outlook\Search" /v "DisableDownloadSearchPrompt" /t reg_dword /d 0x1 /f
reg add "HKCU\Software\Microsoft\Office\12.0\Outlook\Options\RSS" /v "SyncToSysCFL" /t reg_dword /d 0x0 /f

reg delete HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Outlook\Setup\First-Run
reg delete HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Outlook\Setup\FirstRun


GOTO delete.existing.network.drive.mappings

:OFFICE2003
REM Do your Actions here
set OutlookTimeout=10
reg delete "HKCU\Software\Microsoft\Office\11.0\Outlook\Setup" /v "First-Run" /f
reg delete "HKCU\Software\Microsoft\Office\11.0\Outlook\Setup" /v "FirstRun" /f
reg add "HKCU\Software\Microsoft\Office\11.0\Outlook\Setup" /v "ImportPRF" /d "\\w2k3fs01\login\outlook.PRF" /f
GOTO delete.existing.network.drive.mappings

:delete.existing.network.drive.mappings
net use i: /delete
net use k: /delete
net use L: /delete
net use m: /delete
net use n: /delete
net use q: /delete
net use s: /delete
net use t: /delete
net use u: /delete

exit

Open in new window

**SUCCESS**

Made a slight modification in the script and the reg keys were made.

 
REG QUERY "HKLM\SOFTWARE\Microsoft\Office\12.0" | GOTO OFFICE2007
) || (
REG QUERY "HKLM\Software\Microsoft\Office\11.0" | GOTO OFFICE2003
)

Open in new window

LOL!!  My ignorance is amazing at times.  :)

So I did a revert on the virtual pc to see what happens if I run the test script and I'm guessing since I did a horrible hatchet job on the script, it created the reg keys for the 12.0 without even calculating whether or not 12.0 was in the HKLM.  Ugh.  How I found my way to work this morning is a mystery.

Ok.  So back to waiting (patiently) for help since the script I butchered does in fact not work.  Thanks for everyone's help and patience.
Could you post your script again? And then tell what does not work? I'm confused ...
It works.  Man that was a monster!  I took some previous code, mixed in mine and arrived at this which works if Office 2003 is installed or Office 2007 or the compatibility pack for 2007 (which I didn't think of until you mentioned it).


2>nul >nul REG QUERY "HKLM\SOFTWARE\Microsoft\Office\12.0\Outlook" && (

reg add "HKCU\Software\Microsoft\Office\12.0\Outlook\Setup" /v "ImportPRF" /d "\\w2k3nas01\public\office.2007\prf\cs.outlook07.PRF" /f
reg add "HKCU\Software\Microsoft\Office\12.0\Common\General" /v "ShownOptIn" /t reg_dword /d 0x1 /f
reg add "HKCU\Software\Microsoft\Office\12.0\Outlook\Search" /v "DisableDownloadSearchPrompt" /t reg_dword /d 0x1 /f
reg add "HKCU\Software\Microsoft\Office\12.0\Outlook\Options\RSS" /v "SyncToSysCFL" /t reg_dword /d 0x0 /f

reg delete HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Outlook\Setup\First-Run
reg delete HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Outlook\Setup\FirstRun

) || (

set OutlookTimeout=10
reg delete "HKCU\Software\Microsoft\Office\11.0\Outlook\Setup" /v "First-Run" /f
reg delete "HKCU\Software\Microsoft\Office\11.0\Outlook\Setup" /v "FirstRun" /f
reg add "HKCU\Software\Microsoft\Office\11.0\Outlook\Setup" /v "ImportPRF" /d "\\w2k3fs01\login\outlook.PRF" /f

)

Open in new window

But that script does not work with 2010! :-))
And I will be looking for you and your nemisis, I mean, fellow EE'er DrU to help when that time comes.  hahaha!!

Thanks everyone for putting up with my rambling code, broad question and spam like responses.  No more coffee and alergy medicine.

It was great to work with you all and look forward to doing it again soon!

Thanks,
Aaron