Guy’s Scripting Ezine 8 – Registry Script

Contents of Guy’s Scripting Ezine 8 – Registry Script

This Week’s Secret

This Week’s Secret is to take you behind the scenes and show you how I create a script by combining skills from different areas.  If your goal is to write your own scripts then it’s handy to have a working knowledge of these areas: VBScript (WSH), Windows 2000/3 (utilities), the registry (Regedit), Active Directory (LDAP).  I must stress that you just need working knowledge, not expert status; moreover, you do not need to know all of the areas for all of the scripts – build up gradually.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Scripts to alter registry settings

I have had several requests to create VBScripts which alter the registry settings.  Once you have mastered the technique, the scope for registry scripting is nearly as big as the registry itself.  What I want to concentrate on are skills so that you will be able to take a setting like a check box in Explorer, find where that setting is stored in the registry, then write a script to configure it.

Example – to Empty Temporary Internet Files folder

This week I had a request to create a logon script that modified the Internet Explorer settings.  My initial thought was – use a Group Policy, but there was no group policy to control this check box.

WINDIFF and REGEDIT

I admit frustration crept in when I altered the setting in the registry and nothing happened in Internet Explorer, or when I cleared a tick in the Internet Explorer and nothing seemed to change in the registry.

So I went away, had a cup of tea mowed the lawn, then an idea hit me.  Export the registry, use the WINDIFF utility and trace where the settings are really found.  Here is the technique I used:

  1. Change the settings in Internet Explorer – ‘Empty Temporary Internet Files folder when browsing’ so that there is a tick in the box.
  2. Over to Regedit.  Navigate to the section of the registry where that setting is likely to be. HKEY_CURRENT_USER_Software_Microsoft, right-click, then Select Export on the shortcut menu.  Choose a suitable filename e.g. tick.reg. 
  3. Then go to Internet explorer remove the tick from the Export Temporary… checkbox.
  4. Back to Regedit and Export again, this time name your file – notick.reg.
  5. Finally, launch WINDIFF open both files and spot the changes in the two files. 
  6. Here is a link for your copy of WINDIFF (also more info on WINDIFF)

The Goal was to make sure that the Internet Explorer deleted all temporary files on exit. IE, Tools, Internet Options, Advanced, Security, Empty Temporary Internet Files on Exit.

RegWrite – WSH Method

The actual scripting part is controlled by a method called RegWrite (Remember other methods like MapNetworkDrive?)  Once WINDIFF told me the correct registry path, it is a matter of taking care with the syntax and using REG_DWORD (not REG_SZ).

There was one last trap to get the tick to appear in Internet Explorer you need to set Persistent = 0.  (Intuition may tell you it should be =1) "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Cache\Persistent"

Traps for your Script:  Empty Temporary Internet Files

  1. HKEY_Current_User (not HKEY_Local_Machine)
  2. Internet Settings\cache (not Internet\Cache)
  3. Persistent is added to the last part of the path \Cache\Persistent
  4. Persistent = 0 (Not =1)

Script code:

‘ VBScript – contact [email protected]
‘ The script writes to the registry value Empty Temporary Internet Files
‘ The key Method is RegWrite.  RegLocate is the name of the variable
‘ Note: REG_DWORD (not REG_SZ)
‘ Note: Logic of 0 (zero = off) means persistent ON – strange but true!

Dim WSHShell, RegLocate, RegLocate1
Set WSHShell = WScript.CreateObject("WScript.Shell")
On Error Resume Next
RegLocate = "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Cache\Persistent"
‘ Take great care with the line above
‘ Note the space between \Internet and Settings\

WSHShell.RegWrite RegLocate,"0","REG_DWORD"

WScript.Quit ‘ Tells the script to stop and exit.
 

Troubleshooting Resources

See more about registry scripts

Registry  • Win Registry Hacks  •PowerShell Registry  • Ezine 8 Registry  • Ezines  •Tool Kit

Ezine 62 Registry  •Ezine 63 Registry  • Ezine 64 Write Registry  • Ezine 119 Registry shortcuts 

Ezine 121 CachedLogons  • Ezine 136 remove shortcut  •Ezine 137 Remove shortcut