Documente Academic
Documente Profesional
Documente Cultură
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
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.
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.
REGEDIT4
[-HKEY_CURRENT_USER\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.
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.
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.
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.
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 _=ˆ&
: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.
:: 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
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
:: 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.
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/
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
:: Read relevant registry entries and store them in a temporary REG file
REGEDIT /E "%Temp%.\Screen.reg" "HKEY_CURRENT_CONFIG\System\CurrentControlSet\Services"
:: Display results
ECHO.
SET VideoCard
SET XResolution
SET YResolution
SET BitsPerPel
SET VRefresh
:: 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.