Scripting Home > Code Snippets

Reading and Writing Registry Values Popular

Language: VBScript

Version: 1.0 (June 8, 2005)

Download: rewrite.vbs (Use "Save Target As..." with IE5.5)

This example scripts shows how to read a registry value, query the user, then write a registry value based on user input.

To copy the script either use the download link above or cut and paste the following code into a text document and name it rewrite.vbs

' Begin code for rewrite.vbs
' Downloaded from the Scripting Guide for Windows
' http://www.pctools.com/guides/scripting/
' Version: 1.0 (June 8, 2005)

Option Explicit

Dim WSHShell, RegKey, ScreenSaver, Result

Set WSHShell = CreateObject("WScript.Shell")

RegKey = "HKCU\Control Panel\Desktop\"

ScreenSaver = WSHShell.RegRead (regkey & "ScreenSaveActive")

If ScreenSaver = 1 Then 'Screen Saver is Enabled

Result = MsgBox("Your screen saver is currently active." & _
vbNewLine & "Would you like to disable it?", 36)

If Result = 6 Then 'clicked yes
WSHShell.RegWrite regkey & "ScreenSaveActive", 0
End If

Else 'Screen Saver is Disabled

Result = MsgBox("Your screen saver is currently disabled." & _
vbNewLine & "Would you like to enable it?", 36)

If Result = 6 Then 'clicked yes
WSHShell.RegWrite regkey & "ScreenSaveActive", 1
End If

End If

' End code

Disclaimer: This information is provided on an "as is" basis and all risk is with you. PC Tools Limited makes no warranties, express, implied or statutory, as to any matter whatsoever.