Solved

VB Script to check registry for existing key and add if not exist

Posted on 2008-10-09
2
993 Views
Last Modified: 2012-05-05
Hi,

I have a little amount of vbs skill but am in a hurry today, so 500 big ones to the person who helps me write a vbs to do the following in accordance with the M$ article

http://support.microsoft.com/kb/932842/en-us?spid=2530&sid=95

The entry needs to be in the following subkey

HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Word\Options

the key should be

UpdateHeaderFooter

with a value of 1

Of course I'd like to check the key exists before the operation so that it isnt done unessecarily

Many Thanks
0
Comment
Question by:BarkersIT
2 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 250 total points
ID: 22679499
Paste the script below into a text file with a .vbs extension.  Running it will set the desired value.  Registry writes are fast enough that checking whether the value exists first isn't worthwhile.

You can also do this in batch with the command
reg add HKCU\Software\Microsoft\Office\11.0\Word\Options /v UpdateHeaderFooter /t REG_DWORD /d 1 /f


Const HKEY_CURRENT_USER = &H80000001

 

Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")

 

strKeyPath = "Software\Microsoft\Office\11.0\Word\Options"

strValueName = "UpdateHeaderFooter"

strValue = 1
 

objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue

Open in new window

0
 
LVL 65

Assisted Solution

by:RobSampson
RobSampson earned 250 total points
ID: 22682009
Hi, if you want to check the value first (although there's not much of a performance difference there for a single DWord value) you can use this.

Regards,

Rob.
Const HKEY_CURRENT_USER = &H80000001

 

Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv")

 

strKeyPath = "Software\Microsoft\Office\11.0\Word\Options"

strValueName = "UpdateHeaderFooter"

strValue = 1
 

intReturn = objRegistry.GetDWordValue(HKEY_CURRENT_USER, strKeyPath, strValueName, strCurrentValue)

If intReturn = 0 Then

	If CInt(strCurrentValue) <> CInt(strValue) Then

		objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue

	Else

		WScript.Echo strKeyPath & "\" & strValueName & " is already set to " & strCurrentValue

	End If

Else

	WScript.Echo strKeyPath & "\" & strValueName & " is not set.  Setting to " & strValue

	objRegistry.SetDWORDValue HKEY_CURRENT_USER, strKeyPath, strValueName, strValue

End If

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now