Use wsh to modify the Registry

Source: Internet
Author: User

Use wsh to modify the Registry

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.

Wsh written in VBScript Program 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 ")

These Code Creates an operationregistry object that can communicate with the operating system.



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 \")

Returns a null value)

Operationregistry. regwrite "hkcr \. xxf \", default

Create the primary key. xxf under the root key hkey_classes_root, and set its default value to null.

Operationregistry. regwrite "hkcr \. xxf \", "xxffile"

Create a primary key under the root key hkey_classes_root and set its default value to "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 rege_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 the data under the key value is, 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 key 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

Hidden. vbs

Dim hiddenarrowicon

Set hiddenarrowicon = wscript. Createobject ("wscript. Shell ")

Dim regpath1, regpath2

Regpath1 = "hkcr \ lnkfile \ isw.cut"

Regpath2 = "hkcr \ piffile \ isshortcut"

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) to disable the "run" function in the "Start" menu.

Call change (startmenu_find) to disable the "Search" function in the "Start" menu.

Call change (startmenu_close) disable the "Disable 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 named autorun.exe in the C: \ myfilefolder.

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"

Full path File Name of the self-starting Program

Autorunprogram. Write regpath & key_name, key_data, type_name

In the Startup Group, the automatic Startup Program autorun.exe

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.