Sunteți pe pagina 1din 10

REGEDIT

Warning: Before tweaking the registry, make sure you have a full backup of your system. If anything goes wrong, you may end
up losing all your data and reinstalling Windows.
As Microsoft states it, editing the registry "... is not supported by Microsoft. Use this method at your own risk."

Windows NT (and 2000) users, read Microsoft's Knowledge Base article Q318149 to learn How to Maintain Current Registry
Backups in Windows NT 4.0 and Windows 2000.

General Syntax:
GUI mode:
REGEDIT.EXE

Import (merge) a .REG file:


REGEDIT.EXE [ /L:system | /R:user ] [ /S ] importfile.REG

Export to a (.REG) file:


REGEDIT.EXE [ /L:system | /R:user ] /E exportfile "registry_key"

Compress the registry (Windows 98 only):


REGEDIT.EXE [ /L:system | /R:user ] /C

Parameters:

importfile.REG
.REG file to be imported (or "merged")

exportfile.REG
File name the information should be written to

"registry_key"
Registry key to be exported, e.g. "HKEY_CLASSES\ROOT\*\shell"

/S
Silent, i.e. hide confirmation box when importing files

/E
Export registry file

/L:system
Specify the location of the system.dat to use

/R:user
Specify the location of the user.dat to use

/C
Compress [filename] (Windows 98 only)

REGEDIT is usually known as a GUI tool to search or edit the Windows registry.
I would not have mentioned it here, however, if it couldn't be used in unattended mode as well.

This page focuses on reading and editing the registry in unattended mode only.

Microsoft's NT 4 Workstation and Server Resource Kits come with REG.EXE, a utility that makes reading (or editing) NT's registry
easier.
In Windows 2000, REG.EXE is available on the installation CD-ROM, but has to be installed manually (on the CD-ROM run
"\SUPPORT\TOOLS\SETUP.EXE").
As of Windows XP, REG.EXE is a native command.

REG.EXE can read a single key and value directly, without the need for a temporary file.
Learn more about using REG.EXE.

Adding and editing (importing) registry entries


Adding items to the registry requires a *.REG file:

REGEDIT [ /S ] addsome.REG

The /S swith is optional, it skips the message dialogs before and after the import of the *.REG file.

Since NT 4 .REG files are in readable ASCII, they may be created "on the fly" by our batch files.
This is demonstrated in the DefOpen example in the Examples section.

Owners of a copy of the NT 4 Resource Kit or Windows 2000/XP/Server 2003 can also use REG.EXE to add or edit registry entries.

Removing registry entries


To remove an entire "tree" from the registry using REGEDIT and a .REG file, just add a minus sign before the tree name:

REGEDIT4

[-HKEY_CURRENT_USER\DummyTree]

will remove the entire tree "DummyTree".

To remove an individual item from the registry, place the minus sign after the equal sign:

REGEDIT4

[HKEY_CURRENT_USER\DummyTree]
"ValueToBeRemoved"=-
will remove the individual value "ValueToBeRemoved" from "DummyTree".
This is demonstrated in the uniqueid.bat example below, a batch file that forces a new LANDesk agent ID.

How to remove an individual registry key or value using *.INF files and RUNDLL is explained by Bill James.

NT 4 users who own a copy of the NT 4 Resource Kit can also use REG.EXE to remove registry entries.
In Windows 2000 and later REG.EXE is a native tool.

Reading (exporting) from the registry


REGEDIT's /E switch can be used to export a registry key:

REGEDIT /E d:\path\filename.REG "HKEY_XXXX\Whatever Key"

This will write the registry key "HKEY_XXXX\Whatever Key" and its subkeys to a file named d:\path\filename.REG

The resulting (ASCII or UniCode) file will contain the entries in the format "key"="value", which can be stripped and parsed
using Laurence Soucy's CHOICE trick (How-to #4, second method) for MS-DOS 6 and Windows 9*, NT's FOR /F or the more
generic TYPE and FIND commands.

Instead of a file name, some device names can be used:

REGEDIT /E PRN "HKEY_XXXX\Whatever Key"

will print the selected key.


Unfortunately, this won't work for CON (console or display).

NT 4 users who own a copy of the NT 4 Resource Kit can also use REG.EXE to read the registry.
In Windows 2000 and later REG.EXE is a native tool.

Self-contained registry scripts


In .REG files, every line after the first one that starts with a semicolon (;) is treated as comment.
Batch files completely ignore the semicolons before commands.
So the following batch file will use itself as a .REG file:

REGEDIT4

; @ECHO OFF
; CLS
; REGEDIT.EXE /S "%~f0"
; EXIT

[HKEY_CURRENT_USER\Software\Test]
"TestVal"="Succeeded"

The REGEDIT4 line is required, and must be the first line, otherwise REGEDIT.EXE won't accept the script as a valid .REG file.
However, it will generate an error message when running as a batch file, hence the CLS command to wipe the error message from the
screen.
To prevent more error messages, EXIT is used to abort the batch file immediately after the REGEDIT.EXE command.

Use this technique to add or remove registry keys and values.

Examples

Windows 95/98
A fine example of reading the registry in Windows 95/98 is CDROM.BAT, which also demonstrates the use of CHOICE to strip
characters (in this case the quotes) from a string.

DefOpen.bat
(Windows 95, 98, ME, NT 4 & 2000 only)

The following batch file, DEFOPEN.BAT, will create a temporary .REG file and then use REGEDIT to merge it.
Running this batch file once (!) will create a default association to Notepad.exe (double-clicking a file without a file association will
open the file in Notepad) and add three menu entries to the right mouse button menu: "Open with Notepad", "Print with Notepad" and
"Command Prompt Here". The latter will be shown, no matter wether a file or directory is clicked.
[Notepad registry tip courtesy of Regedit.com]

@ECHO OFF
:: No parameters required
IF NOT [%1]==[] GOTO Syntax

:: Choose the correct command processor for the current operating system
SET _cmd=
:: Variable to add shortcut to menu entry (NT only,
:: since COMMAND.COM cannot echo an ampersand)
SET _=
ECHO.%COMSPEC% | FIND /I "command.com" >NUL
IF NOT ERRORLEVEL 1 SET _cmd=command.com /e:4096
ECHO.%COMSPEC% | FIND /I "cmd.exe" >NUL
IF NOT ERRORLEVEL 1 SET _cmd=cmd.exe
IF [%_cmd%]==[cmd.exe] SET _=ˆ&

:: Create a temporary .REG file


> %TEMP%.\DEFOPEN.REG ECHO REGEDIT4
>>%TEMP%.\DEFOPEN.REG ECHO.

:: Open With Notepad doesn't work in XP


SET Skip=0
VER | FIND /I "XP" >NUL
IF NOT ERRORLEVEL 1 SET Skip=1
VER | FIND /I "Server 2003" >NUL
IF NOT ERRORLEVEL 1 SET Skip=1
IF "%Skip%"=="1" ECHO Skipping "Open with Notepad" entry
IF "%Skip%"=="1" GOTO Print
ECHO Adding "Open with Notepad" entry
>>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\*\shell\open]
>>%TEMP%.\DEFOPEN.REG ECHO @="%_%Open with Notepad"
>>%TEMP%.\DEFOPEN.REG ECHO.
>>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\*\shell\open\command]
>>%TEMP%.\DEFOPEN.REG ECHO @="notepad.exe \"%%L\""
>>%TEMP%.\DEFOPEN.REG ECHO.

:Print
ECHO Adding "Print with Notepad" entry
>>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\*\shell\print]
>>%TEMP%.\DEFOPEN.REG ECHO @="%_%Print with Notepad"
>>%TEMP%.\DEFOPEN.REG ECHO.
>>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\*\shell\print\command]
>>%TEMP%.\DEFOPEN.REG ECHO @="notepad.exe /p \"%%L\""
>>%TEMP%.\DEFOPEN.REG ECHO.

:: If neither COMMAND.COM nor CMD.EXE then skip this step


IF [%_cmd%]==[] ECHO Skipping "Command Prompt Here" entry
IF [%_cmd%]==[] GOTO Merge

ECHO Adding "Command Prompt Here" entry


SET Pushd=cd
IF "%Skip%"=="1" SET Pushd=pushd
VER | FIND /I "Windows 2000" >NUL
IF NOT ERRORLEVEL 1 SET Pushd=pushd

:: Add Command Prompt Here for files


>>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\*\shell\prompt]
>>%TEMP%.\DEFOPEN.REG ECHO @="Command Prompt Here"
>>%TEMP%.\DEFOPEN.REG ECHO.
>>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\*\shell\prompt\command]
>>%TEMP%.\DEFOPEN.REG ECHO @="%_cmd% /k %Pushd% \"%%L\\..\""
>>%TEMP%.\DEFOPEN.REG ECHO.
:: Add Command Prompt Here for directories
>>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\Directory\shell\prompt]
>>%TEMP%.\DEFOPEN.REG ECHO @="Command Prompt Here"
>>%TEMP%.\DEFOPEN.REG ECHO.
>>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\Directory\shell\prompt\command]
>>%TEMP%.\DEFOPEN.REG ECHO @="%_cmd% /k %Pushd% \"%%L\""
>>%TEMP%.\DEFOPEN.REG ECHO.
:: Add Command Prompt Here for drives
>>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\Drive\shell\prompt]
>>%TEMP%.\DEFOPEN.REG ECHO @="Command Prompt Here"
>>%TEMP%.\DEFOPEN.REG ECHO.
>>%TEMP%.\DEFOPEN.REG ECHO [HKEY_CLASSES_ROOT\Drive\shell\prompt\command]
>>%TEMP%.\DEFOPEN.REG ECHO @="%_cmd% /k %Pushd% \"%%L\""
>>%TEMP%.\DEFOPEN.REG ECHO.

:: Merge the temporary .REG file


:Merge
START /WAIT REGEDIT /S %TEMP%.\DEFOPEN.REG

:: Delete the temporary .REG file


DEL %TEMP%.\DEFOPEN.REG

:: Ready
GOTO End

:Syntax
ECHO.
ECHO DefOpen.bat, Version 3.00 for Windows 95 .. Windows Server 2003
ECHO Adds a default file association: double-clicking a file without a file
ECHO association will open the file in Notepad.
ECHO Also adds three new entries to Explorer's context menu: "Open with Notepad",
ECHO "Print with Notepad" and "Command Prompt Here".
ECHO.
ECHO Usage: DEFOPEN
ECHO.
ECHO Notes: In Windows 2000 and later, "Command Prompt Here" will also work for
ECHO UNC paths by using Reinhard Irnberger's PUSHD trick; the price to pay
ECHO for this functionality is that you need to close the window with the
ECHO POPD command, otherwise a network mapping will persist.
ECHO "Open With Notepad" will not be added in XP or later, as it won't work.
ECHO Notepad registry tip courtesy of Regedit.com (http://www.regedit.com)
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com

:: Clean up variables and quit


:End
SET _cmd=
SET _=

LANDesk unique ID
The following example deletes a file and a registry entry to force Intel LANDesk agent software to create a new unique ID.
Run it once on any PC containing Intel's LANDesk agent software that was installed using any "disk cloning" technique (like Norton
Ghost, PowerQuest's Drive Image, or tape backups).

@ECHO OFF
:: No command line parameters required
IF NOT "%1"=="" GOTO Syntax

:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
:: Adjust the LANDesk server name and the drive and directory if necessary ::
:: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: :: ::
SET LANDeskServer=MyLDServer
C:
CD\LDClient

ECHO Removing old LANDesk agent ID

:: Delete old ID file (adjust drive if necessary)


IF EXIST C:\LDIScan.cfg ATTRIB -h -r -s C:\LDIScan.cfg
IF EXIST C:\LDIScan.cfg DEL C:\LDIScan.cfg

:: Delete old ID from the registry (Local Machine)


:: Create a temporary REG file
> %TEMP%.\UniqueID.reg ECHO REGEDIT4
>>%TEMP%.\UniqueID.reg ECHO.
>>%TEMP%.\UniqueID.reg ECHO [HKEY_LOCAL_MACHINE\SOFTWARE\Intel\LANDesk\Common Api]
>>%TEMP%.\UniqueID.reg ECHO "UniqueID"=-
>>%TEMP%.\UniqueID.reg ECHO.

:: Check if previous ID was stored for Current User and if so, delete it
IF EXIST %TEMP%.\_Unique_.REG DEL %TEMP%.\_Unique_.REG
REGEDIT /E %TEMP%.\_Unique_.REG "HKEY_CURRENT_USER\SOFTWARE\Intel\LANDesk\LDWM\LDIScan32"
TYPE %TEMP%.\_Unique_.REG | FIND /I "Last Device ID" >NUL 2>&1
IF ERRORLEVEL 1 GOTO Next
>>%TEMP%.\UniqueID.reg ECHO [HKEY_CURRENT_USER\SOFTWARE\Intel\LANDesk\LDWM\LDIScan32]
>>%TEMP%.\UniqueID.reg ECHO "Last Device ID"=-
>>%TEMP%.\UniqueID.reg ECHO.

:: Repeat for Default User


:Next
IF EXIST %TEMP%.\_Unique_.REG DEL %TEMP%.\_Unique_.REG
REGEDIT /E %TEMP%.\_Unique_.REG "HKEY_USERS\.Default\SOFTWARE\Intel\LANDesk\LDWM\LDIScan32"
TYPE %TEMP%.\_Unique_.REG | FIND /I "Last Device ID" >NUL 2>&1
IF ERRORLEVEL 1 GOTO Merge
DEL %TEMP%.\_Unique_.REG
>>%TEMP%.\UniqueID.reg ECHO [HKEY_USERS\.Default\SOFTWARE\Intel\LANDesk\LDWM\LDIScan32]
>>%TEMP%.\UniqueID.reg ECHO "Last Device ID"=-
>>%TEMP%.\UniqueID.reg ECHO.

:: Merge (import) the REG file to delete the UniqueID entry


:Merge
REGEDIT /S %TEMP%.\UniqueID.reg
:: Delete the temporary REG file
DEL %TEMP%.\UniqueID.reg

ECHO Creating new LANDesk agent ID

:: Start inventory scan to create new ID


LDISCN32.EXE /NTT=%LANDeskServer%:5007 /S="%LANDeskServer%" /I=\\%LANDeskServer%\LDLOGON\ldappl.

ECHO Done
IF EXIST %TEMP%.\_Unique_.REG DEL %TEMP%.\_Unique_.REG
SET LANDeskServer=
CD\
GOTO End

:Syntax
ECHO UniqueID.bat, Version 2.00 for 32-bit Windows
ECHO Create a new unique ID for Intel LANDesk agent
ECHO.
ECHO If a computer is installed using a harddisk image (like Norton Ghost
ECHO or PowerQuest's Drive Image) all "clones" will have the same LANDesk
ECHO agent ID if LANDesk agent was installed when the image was created.
ECHO However, the LANDesk agent needs a unique ID to function properly.
ECHO You can force a new ID by reinstalling the LANDesk agent software,
ECHO but running this batch file can force a new unique ID just as well.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com

:End

Country3.bat
(NT 4 only)

The following example, COUNTRY3.BAT for Windows NT 4, clearly demonstrates how to read registry settings using REGEDIT:
@ECHO OFF
:: Country.bat, Version 3.01 for Windows NT 4
:: Displays several country related settings.
:: This version uses native NT 4 commands only.
:: Written by Rob van der Woude
:: http://www.robvanderwoude.com
:: Keyboard trick by Tom Lavedas
:: http://web.archive.org/web/20070222072741rn_1/members.cox.net/tglbatch/

:: Export International settings from registry to a temporary file


START /W REGEDIT /E %Temp%.\international.reg "HKEY_CURRENT_USER\Control Panel\International"
:: Read several lines from the temporary files
:: and store these settings as environment variables
FOR /F "tokens=1* delims==" %%A IN ('TYPE %Temp%.\international.reg ^| FIND "iCountry"') DO SET
SET iCountry=%iCountry:"=%
SET iCountry
FOR /F "tokens=1* delims==" %%A IN ('TYPE %Temp%.\international.reg ^| FIND "sCountry"') DO SET
SET sCountry=%sCountry:"=%
SET sCountry
FOR /F "tokens=1* delims==" %%A IN ('TYPE %Temp%.\international.reg ^| FIND "sLanguage"') DO SET
SET sLanguage=%sLanguage:"=%
SET sLanguage
:: Remove temporary file
DEL %Temp%.\international.reg
:: The KEYB trick was posted to the alt.msdos.batch.nt newsgroup by Tom Lavedas
FOR /F "tokens=1* delims=:" %%A IN ('KEYB ^| FIND ":" ^| FIND /V "CON"') DO SET Keyboard=%%B
SET Keyboard=%Keyboard:~1%
SET Keyboard
:: Check code page used
FOR /F "tokens=1* delims=:" %%A IN ('CHCP') DO SET /A CodePage = %%B
SET CodePage

GetRes.bat
(Windows NT 4/2000 only)

The following example, GETRES.BAT (Version 3.00) for Windows NT 4/2000, also demonstrates how to read and parse registry
settings using REGEDIT:

@ECHO OFF
:: No command line parameters please
IF NOT [%1]==[] GOTO Syntax

:: Verify OS version
IF NOT "%OS%"=="Windows_NT" GOTO Syntax

:: Keep variables local


SETLOCAL

:: Set initial values


SET VideoCard=
SET XResolution=
SET YResolution=
SET BitsPerPel=
SET VRefresh=

:: Read relevant registry entries and store them in a temporary REG file
REGEDIT /E "%Temp%.\Screen.reg" "HKEY_CURRENT_CONFIG\System\CurrentControlSet\Services"

:: Process the temporary REG file


FOR /F "tokens=* delims=" %%A IN ('TYPE %Temp%.\Screen.reg ˆ| FIND /V "REGEDIT4" ˆ| FIND /V ""')

:: Display results
ECHO.
SET VideoCard
SET XResolution
SET YResolution
SET BitsPerPel
SET VRefresh

:: Remove temporary file


IF EXIST "%Temp%.\Screen.reg" DEL "%Temp%.\Screen.reg"

:: Done
ENDLOCAL
GOTO:EOF

:Parse01
:: If this is the second card's entry, and the
:: first one was NOT VGA save mode, then quit
IF NOT "%VideoCard%"=="" IF NOT "%VideoCard%"=="%NewCard%" IF /I NOT "%VideoCard%"=="VgaSave" GO
:: Read subroutine's command line parameters as one line
SET Line=%*
:: Remove quotes from the line
SET Line=%Line:"=%
:: Remove spaces from the line
SET Line=%Line: =%
:: Check for and parse video card short name
IF "%Line:~0,6%"=="[HKEY_" FOR /F "tokens=5 delims=\]" %%A IN ('ECHO.%Line%') DO IF NOT "%%A"=="
:: If this is the second card's entry, and the
:: first one was NOT VGA save mode, then quit
IF NOT "%VideoCard%"=="" IF NOT "%VideoCard%"=="%NewCard%" IF /I NOT "%VideoCard%"=="VgaSave" GO
:: If not, then save the new video adapter short name
SET VideoCard=%NewCard%
:: Parse all "key=value" lines
FOR /F "tokens=1* delims==" %%A IN ('ECHO.%Line%') DO CALL :Parse02 %%A %%B
:: End of subroutine Parse01
GOTO:EOF

:Parse02
:: Quit if either key or value aren't specified
IF "%2"=="" GOTO:EOF
:: %1 is key
SET InKey=%1
:: %2 is value
SET InValue=%2
:: Remove "Defaultvalue." prefix from key
FOR /F "tokens=2 delims=." %%a IN ('ECHO.%InKey%') DO SET Key=%%a
:: Continue only for these 4 keys, quit otherwise
IF /I NOT "%Key%"=="BitsPerPel" IF /I NOT "%Key%"=="XResolution" IF /I NOT "%Key%"=="YResolution
:: Parse value to check if it is a dword value
FOR /F "tokens=1,2 delims=:" %%a IN ('ECHO.%InValue%') DO (
SET dWord=%%a
SET xValue=%%b
)
:: If so, convert hexadecimal value to decimal
IF /I "%dWord%"=="dword" (SET /A Value = 0X%xValue%) ELSE (SET Value=%dWord%)
:: Store the result
SET %Key%=%Value%
:: End of subroutine Parse02
GOTO:EOF

:Syntax
ECHO.
ECHO GetRes.bat, Version 3.00 for Windows NT 4 / 2000
ECHO Read and display video resolution settings from the registry
ECHO.
ECHO Usage: GETRES
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO.

More to explore:
More examples, dealing with printing various file types, can be found here.

A great tool to create VBScript scripts that can modify the registry is the Tweakomatic by the Scripting Guys.

An overview of Registry Entries for Printing.

S-ar putea să vă placă și