Windows Registry

scripting


In just about every document about the Windows Registry, Microsoft states that one should NEVER edit the registry since it can render your system completely unusable. You have been warned.

System configuration should be performed using technologies such as Group Policy Objects and the likes. However, sometimes it is convenient to edit the registry directly, e.g. as a workaround for older systems that can't be managed with Group Policy Objects (e.g. Windows 98), to fix bugs, to change a configuration setting that can't be managed with the tools at hand, etc. If the same edit to the Registry has to be performd repeatedly, or on a larger number of machines, it is convenient to batch or script it.

There are several ways to automate registry modifications :

Here are some examples


sample WSH script in vbs

		'Visual Basic script to modify the Registry		

		Set WshShell = WScript.CreateObject("WScript.Shell")

		'create or update a registry key
		WshShell.RegWrite "HKCU\MyNewKey\", 1 ,"REG_DWORD"
		WshShell.RegWrite "HKCU\MyNewKey\MyValue", "Hello world!"

		'read a registry key
		WScript.Echo WshShell.RegRead("HKCU\MyNewKey\MyValue")
		WScript.Echo WshShell.RegRead("HKCU\MyNewKey\")

		'delete a registry key
		WshShell.RegDelete "HKCU\MyNewKey\MyValue"
		WshShell.RegDelete "HKCU\MyNewKey\"
		

modifying Domain Logon

In this example, we use REGEDIT in a batch file to edit the registry. The syntax of REGEDIT is REGEDIT /S pathname -- where /S means silent and pathname is the text file that contains the registry key

A text file that contains the registry key can have any extension (or no extension at all), but typically the extension .reg is used, which also allows import by right-clicking the icon. We'll concentrate on scripting, however.

To import registry settings, we'll have to make text files that contain the desired key. Multiple keys can be modified, ether by making a batch file that imports a number of reg-files, or by importing one reg-file that contains multiple keys. It all depends on how you want to organize it.

In this example, we modify the domain logon. The registry key in question is in file 'domainlogon.txt' in a subdirectory of the directory where the script resides (.../keys)
the batch file (the command) simply looks like this :


		REGEDIT /S ./keys/domainlogon.txt
		

while the registry key file (domainlogon.txt) in question looks like this :


		REGEDIT4

		[HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\MSNP32\NetworkProvider]
		"AuthenticatingAgent"="your_netbiosdomainname_here"
	
		

Those Registry Key files kan be created or modified with a text editor. To get them right, it's often easier to export an existing key, then modify the text, and import the modified key again. Exporting is done with

		
		REGEDIT /E path_name_of_file "key_to_export"	
		

so, to export the [HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\MSNP32\NetworkProvider] to /setup/logon.txt :


		REGEDIT /E ./setup/logon.txt "hKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\MSNP32\NetworkProvider"
		

Moving default folders

Here is a script to move "Ducuments and Settings" and "Program Files" to a new location, by editing the registry. Batch file using the WinXP / Server 2003 "REG" command.


Koen Noens
July 2nd, 2005