Modify the Registry in vbs

Source: Internet
Author: User

Modify the Registry in vbs 

Hosts file

Command Format: cscript filename. vbs.

Create object

To modify the registry using VBScript, you must first create an object that can communicate with the operating system, then use various methods of the object to operate the registry, and create methods and cells for this object.

Format:

'The following Code creates the object registryobj that can communicate with the operating system
Dim registryobj
Set registryobj = wscript. Createobject ("wscript. Shell ")
  
Object Method

The above object does not mean that the registry can be operated immediately. We must also find out several important methods for this object to operate on the registry.
1. regread
2. Write the Registry to regwrite
3. Delete registry regdelete
In addition, wsh has two common methods:
Wscript. Echo () is used to display a string of text information, which is equivalent to msgbox () in VB ().
Wscript. Quit () is used to exit the VBScript program.

Method Parameters

The preceding three operations, regread, regwrite, and regdelete, must be performed with parameters, and the number and form of parameters for these operations are different. One of them is common and necessary.

The parameters are described as follows:
This parameter can be called a "path parameter". It includes the root key, primary key path, and key value. Each part is represented as follows:
Root Key:
The root key can be expressed in two ways.
Method 1: Use a string in the registry, for example:
Hkey_classes_root, HKEY_CURRENT_USER, etc.
Method 2: The name is abbreviated to four letters. The first two are HK, and the last two are the first letters of the root key word. For example:
The Root Key hkey_classes_root is hkcr, and the root key HKEY_CURRENT_USER can be expressed as hkcu.
Primary Key Path:
The primary key path is the primary key location of the target key in the registry. Each primary key is separated by a "/" character.

For example: "software/Microsoft/Windows/CurrentVersion/policies /"
Key value:
The key-value parameter is directly followed by the Primary Key Path. For example, a complete path is as follows:
"Hkcr/software/Microsoft/Windows/CurrentVersion/policies/norun"

Detailed description

  1. Detailed regread operations

The read operation regread is mainly used to read the default value or key value of the primary key in the registry. We can send the read data to the corresponding variable, and then use the msgbox () function in VB.

The data is displayed, which enables you to read the data in the Registry (you can also use the operationregistry method popup () to send the read data to the screen). For example:
  
'Read. vbs (Save the following code as a read. vbs file)
  
Dim operationregistry
Set operationregistry = wscript. Createobject ("wscript. Shell ")
Dim read_data1, read_data2
'Read the default value of the. xxf primary key under the root key hkey_classes_root and send the data to the variable read_data1.
Read_data1 = operationregistry. regread ("hkcr/test /")
'Read the data of the value key value under the. xxf primary key and send the data to the variable read_data2.
Read_data2 = operationregistry. regread ("hkcr/test/value ")

Msgbox ("default =" & read_data1 & "value =" & read_data2)

 2. regwrite Operation Details

The write operation regwrite is mainly used to create a primary key or key value in the Registry and assign them an initial value. This operation can also be performed on the existing primary key or key value in the registry.

Data modification. Therefore, the parameter structure of the write operation is more complex than that of the read operation. It requires not only path parameters, but also an initial value and type parameter.
First, let's look at the initial value parameter. this parameter is essential for write operations. It can be null but cannot be saved. When a primary key is created, the initial value parameter is assigned to the default value of the primary key.

Value. When you create a key value, the initial value parameter becomes the initial data of the new key value. The initial value type is determined by the Type parameter. There are three main types:

(1) REG_SZ: struct type. This type is the default type.
(2) REG_DWORD: dubyte type.
(3) REG_BINARY: binary.

The above three types are the most useful in 1st and 2nd, and the 3rd types can be replaced by 2nd in some cases. The assignment methods for these three types are as follows:
For the REG_SZ type: directly use strings, such as "text" and "string ".
REG_DWORD and REG_BINARY are assigned values in two ways.

I) it is expressed in decimal number, for example, 0, 1.
Ii) It is represented by a hexadecimal number, such as 0x12 and 0xff. See the following example:

'Write. vbs
Dim operationregistry
Set operationregistry = wscript. Createobject ("wscript. Shell ")

'Get a null value (null)
Default = operationregistry. regread ("hkcr /")

'Create the primary key. xxf under the root key hkey_classes_root, and leave it blank by default.
Operationregistry. regwrite "hkcr/. xxf/", default

'New primary key. xxf under the root key hkey_classes_root, and set its default value? Quot; xxffile"
Operationregistry. regwrite "hkcr/. xxf/", "xxffile"

'Create a string-type key value value1 under primary key. xxf, and set its initial value to "string"
Operationregistry. regwrite "hkcr/. xxf/value1", "string"

'Create a REG_DWORD key value value2 under primary key. xxf and set its initial value to 1.
Operationregistry. regwrite "hkcr/. xxf/value2", 1, "REG_DWORD"

'Create a binary key value value3 under primary key. xxf, and set its initial value to a hexadecimal ff.
Operationregistry. regwrite "hkcr/. xxf/value3", 0xff, "REG_BINARY"

3. regdelete Operation Details

The delete operation regdelete is mainly used to delete the existing primary key or key value in the registry. This operation is an extremely dangerous operation. It can put the primary key or key value in the Registry without mercy.

"Cut" in, no matter how important data under the key value, it can be unobstructed, so be careful when using this operation.
The parameter format of the delete operation is almost identical to that of the read operation, except that the delete operation does not need to send the return value of the operation to a variable,

For example:

'Delete. vbs
Dim operationregistry
Set operationregistry = wscript. Createobject ("wscript. Shell ")
'Delete the value under the. xxf primary key
Operationregistry. regread ("hkcr/. xxf/value ")

'Delete The. xxf primary key under the root key hkey_classes_root.
Operationregistry. regread ("hkcr/. xxf /")

Application Instance

1. Read the "computer name" of the Local Machine"
'Readcomputername. vbs

Dim readcomputername
Set readcomputername = wscript. Createobject ("wscript. Shell ")
Dim computername, regpath
Regpath = "HKLM/system/CurrentControlSet/control/computername"
Computername = readcomputername. regread (regpath)
Msgbox ("computer name" & computername)

2. Hide the arrow on the shortcut icon

'Den. vbs

Dim hiddenarrowicon
Set hiddenarrowicon = wscript. Createobject ("wscript. Shell ")
Dim regpath1, regpath2
Regpath1 = "hkcr/lnkfile/isw.cut"
Regpath2 = "hkcr/piffile/isw.cut"
Hiddenarrowicon. regdelete (regpath1)
Hiddenarrowicon. regdelete (regpath2)

3. Modify the "Start" menu

'Changestartmenu. vbs

Dim changestartmenu
Set changestartmenu = wscript. Createobject ("wscript. Shell ")
Regpath = "hkcr/software/Microsoft/Windows/CurrentVersion/policies /"
Type_name = "REG_DWORD"
Key_data = 1
 
Startmenu_run = "norun"
Startmenu_find = "nofind"
Startmenu_close = "noclose"
 
Sub change (argument)
Changestartmenu. regwrite regpath & argument, key_data, type_name
Msgbox ("success! ")
End sub
 
Call change (startmenu_run) 'disables the "run" function in the "Start" menu.
Call change (startmenu_find) 'disables the "Search" function in the "Start" menu
Call change (startmenu_close) 'Disable the system function in the Start Menu.

4. Add a self-starting program to Windows

The program runs automatically at startup.
'Addautorunprogram. vbs
'Assume that the program is in the C:/myfilefolder and the file name is autorun.exe.
Dim autorunprogram
Set autorunprogram = wscript. Createobject ("wscript. Shell ")
Regpath = "HKLM/software/Microsoft/Windows/CurrentVersion/run /"
Type_name = "REG_SZ"
Key_name = "autorun"
Key_data = "C:/myfile/autorun.exe"
'The full path File Name of the self-starting Program
Autorunprogram. Write regpath & key_name, key_data, type_name
'Self-starting program autorun.exe In the Startup Group
Msgbox ("success! ")
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.