Thursday, May 7, 2009

Registry Changes via Regedit or VBScript

Recently I decided to make a new batch file that would automate setting up a new workstation that is installed fresh from the disks to prep it for sysprep/imaging. During this process I found that I needed to make several Registry changes. I found 2 ways to do this easily.
1. use the regedit command (haven't tested)
regedit filename.reg
/S to suppress the message box (also see /E and /D for export and delete options)

Within the file the following are valid:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Key]
"name"="data"
To Clear
"name"=""
To remove
"name"=-
Remove entire key
[-HKEY_LOCAL_MACHINE\SOFTWARE\Key]

2. Using VBScript which I find easier
Pick one depending on the key(s) being modified
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003

strComputer = "."
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = "SOFTWARE\Path"
strValueName = "KeyName"
svalue = "Value"
objReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, svalue
or
objReg.SetDWORDValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, svalue
(REG_SZ vs REG_DWORD)

Example:
Const HKEY_USERS = &H80000003
strComputer = "."
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = ".DEFAULT\Control Panel\PowerCfg"
strValueName = "CurrentPowerPolicy"
svalue = "3"
objReg.SetStringValue HKEY_USERS, strKeyPath, strValueName, svalue

No comments:

Post a Comment