Function Reference


RegWrite

Creates a key or value in the registry.

RegWrite ( "keyname" [, "valuename", "type", value] )

Parameters

keyname The registry key to write to. If no other parameters are specified this key will simply be created.
valuename [optional] The valuename to write to.
type [optional] Type of key to write: "REG_SZ", "REG_MULTI_SZ", "REG_EXPAND_SZ", "REG_DWORD", "REG_QWORD", or "REG_BINARY".
value [optional] The value to write.

Return Value

Success: 1.
Failure: 0 and sets the @error flag to non-zero if error writing registry key or value.
@error: 1 = unable to open requested key
 2 = unable to open requested main key
 3 = unable to remote connect to the registry
-1 = unable to open requested value
-2 = value type not supported

Remarks

A registry key must start with "HKEY_LOCAL_MACHINE" ("HKLM") or "HKEY_USERS" ("HKU") or "HKEY_CURRENT_USER" ("HKCU") or "HKEY_CLASSES_ROOT" ("HKCR") or "HKEY_CURRENT_CONFIG" ("HKCC").

When running on 64-bit Windows if you want to write a key or value specific to the 64-bit environment you have to suffix the HK... with 64 i.e. HKLM64.

AutoIt supports registry keys of type REG_BINARY, REG_SZ, REG_MULTI_SZ, REG_EXPAND_SZ, REG_QWORD, and REG_DWORD.

To access the (Default) value use "" (an empty string) for the valuename.

When writing a REG_MULTI_SZ key you must separate each value with @LF. The value must NOT end with @LF unless you want an empty substring (see example).

It is possible to access remote registries by using a keyname in the form "\\computername\keyname". To use this feature you must have the correct access rights.

Depending the starting registry key, #RequireAdmin is needed as for key "HKEY_LOCAL_MACHINE".

Related

RegDelete, RegEnumKey, RegEnumVal, RegRead

Example

Example 1

#include <MsgBoxConstants.au3>

Example()

Func Example()
        ; Check if the registry key is already existing, so as not to damage the user's system.
        RegRead("HKEY_CURRENT_USER\Software\AutoIt_Example", "Key1")

        ; @error is set to non-zero when reading a registry key that doesn't exist.
        If Not @error Then
                MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst the temporary registry key. ""AutoIt_Example"" appears to already exist.")
                Return False
        EndIf

        ; Write a single REG_SZ value to the key "Key1".
        RegWrite("HKEY_CURRENT_USER\Software\AutoIt_Example", "Key1", "REG_SZ", "This is an example of RegWrite")

        ; Write the REG_MULTI_SZ value of "Line 1" and "Line 2". Always append an extra line-feed character when writing a REG_MULTI_SZ value.
        RegWrite("HKEY_CURRENT_USER\Software\AutoIt_Example", "Key2", "REG_MULTI_SZ", "Line 1" & @LF & "Line 2" & @LF)

        ; Write the REG_MULTI_SZ value of "Line 1". Always append an extra line-feed character when writing a REG_MULTI_SZ value.
        RegWrite("HKEY_CURRENT_USER\Software\AutoIt_Example", "Key3", "REG_MULTI_SZ", "Line 1" & @LF)

        ; Display a message to navigate to RegEdit.exe manually.
        MsgBox($MB_SYSTEMMODAL, "", "Open RegEdit.exe and navigate the the registry key ""HKEY_CURRENT_USER\Software\AutoIt_Example"".")

        ; Delete the temporary registry key.
        RegDelete("HKEY_CURRENT_USER\Software\AutoIt_Example")
EndFunc   ;==>Example

Example 2

#include <MsgboxConstants.au3>

;~ #RequireAdmin

; require admin privilege
Local $sKey = "HKEY_LOCAL_MACHINE64\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders"
Local $iRegWrite = RegWrite($sKey, "test", "REG_DWORD", "1")
If $iRegWrite = 0 Then
        MsgBox($MB_ICONWARNING + $MB_TOPMOST, "REQUIRE Admin", "Cannot be created" & @CRLF & @CRLF & $sKey & @CRLF & @CRLF & 'Return = ' & $iRegWrite & @CRLF & '@error = ' & @error & @TAB & '@extended = ' & @extended & ' (0x' & Hex(@extended) & ')') ;### Debug MSGBOX
Else
        MsgBox($MB_TOPMOST, "RegWrite", $sKey & @CRLF & @CRLF & "succesfully created")

        ; clean registry
        RegDelete($sKey, "test")
EndIf

; Does not require admin privilege
$sKey = "HKEY_CURRENT_USER\Software\Test"
$iRegWrite = RegWrite("HKEY_CURRENT_USER\Software\Test", "test", "REG_DWORD", "1")
If $iRegWrite = 0 Then
        ; Should not occur
        MsgBox($MB_ICONERROR + $MB_TOPMOST, "AutoIt Error", "Cannot create" & @CRLF & @CRLF & $sKey & @CRLF & @CRLF & 'Return = ' & $iRegWrite & @CRLF & '@error = ' & @error & @TAB & '@extended = ' & @extended & ' (0x' & Hex(@extended) & ')')
Else
        If IsAdmin() Then
                MsgBox($MB_TOPMOST, "RegWrite", $sKey & " succesfully created")
        Else
                ConsoleWrite("- " & $sKey & " succesfully created" & @CRLF)
        EndIf

        ; clean registry
        RegDelete($sKey, "test")
        RegDelete($sKey)
EndIf