Detailed description of Windows Registry Modification Using VBScript

Source: Internet
Author: User

Have you heard of the well-known WSH? It is short for Windows Script Host, and WSH is a Script command on Windows platform. It has powerful functions, in addition to modifying the Registry described in this article, it uses the JScript and VBScript languages with simple syntax structure, ease of use, and powerful functions to achieve its outstanding functions, it can also access Excel files and communicate with the network. Of course, its biggest advantage is that it can communicate with the operating system, and modifying the registry is only the tip of the iceberg of communication between it and the operating system. It is precisely because of its many advantages and practicality that it is favored by many Windows users. This article will introduce you to the first and second articles, so that you can see the style of WSH.
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, and then use various methods of the object to operate the registry. The method and format of creating this object are as follows:
Dim OperationRegistry
Set OperationRegistry = WScript. CreateObject ("WScript. Shell ")
The above Code creates an object that can communicate with the operating system OperationRegistry
 
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 carry parameters, and the number and form of parameters for these operations are different, next I will talk about one of their common and essential parameters:
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 data of the primary key in the registry. We can send the read data to the corresponding variable, and then use MsgBox () in VB () the function displays the data to read the data in the Registry (you can also use the OperationRegistry method Popup () to send the 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_Data1 = OperationRegistry. RegRead ("HKCR/. xxf /")
'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_Data2 = OperationRegistry. RegRead ("HKCR/. xxf/value ")
'Read the data of the value key value under the. xxf primary key and send the data to the variable Read_Data2.
MsgBox ("Default =" & Read_Data1 & "value =" & Read_Data2)
'Display the read data

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 modify the data of an existing primary key or key value in the registry. Therefore, the parameter structure of the write operation is more complex than that of the read operation, you also need 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. When a new key value is created, the initial value parameter is the initial data of the new key value. the type of the initial value 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 ")
Default = OperationRegistry. RegRead ("HKCR /")
'Get a null value (null)
 
OperationRegistry. RegWrite "HKCR/. xxf/", Default
'Create the primary key. xxf under the root key HKEY_CLASSES_ROOT, and leave it blank by default.
 
OperationRegistry. RegWrite "HKCR/. xxf/", "xxffile"
'New primary key. xxf under the root key HKEY_CLASSES_ROOT, and set its default value? Quot; xxffile"
 
OperationRegistry. RegWrite "HKCR/. xxf/value1", "string"
'Create a string-type key value value1 under primary key. xxf, and set its initial value to "string"
 
OperationRegistry. RegWrite "HKCR/. xxf/value2", 1, "REG_DWORD"
'Create a REG_DWORD key value value2 under primary key. xxf and set its initial value to 1.
 
OperationRegistry. RegWrite "HKCR/. xxf/value3", 0Xff, "REG_BINARY"
'Create a binary key value value3 under primary key. xxf, and set its initial value to a hexadecimal ff.

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 extremely dangerous, it can "cut down" the primary key or key value in the Registry without mercy. No matter how important data is under the key value, it can be freely transmitted, therefore, be careful when using this operation.
The parameter form of the delete operation is almost identical to that of the read operation, but there is a slight difference, that is, 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 ")
OperationRegistry. RegRead ("HKCR/. xxf/value ")
'Delete the value under the. xxf primary key
OperationRegistry. RegRead ("HKCR/. xxf /")
'Delete The. xxf primary key under the root key HKEY_CLASSES_ROOT.

Do not change the existing primary keys or key values in the registry, or delete them because improper write or delete operations are performed on the registry, if the problem is serious, the system will crash! If you really want to do this, please back up the registry.
 
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.