How do I add a multiline REG_SZ string to the registry from the command line?

14,794

Solution 1

You could create a VBScript(.vbs) file and just call it from a batch file, assuming you're doing other things in the batch other than this registry change. In vbscript you would be looking at something like:

set WSHShell = CreateObject("WScript.Shell")  
WSHShell.RegWrite "HKEY_LOCAL_MACHINE\SOMEKEY", "value", "type"

You should be able to find the possible type values using Google.

Solution 2

If you're not constrained to a scripting language, you can do it in C# with

Registry.CurrentUser.OpenSubKey(@"software\classes\something", true).SetValue("some key", "sometext\nothertext", RegistryValueKind.String);

Solution 3

You can import multiline REG_SZ strings containing carriage return (CR) and linefeed (LF) end-of-line (EOL) breaks into the registry using .reg files as long as you do not mind translating the text as UTF-16LE hexadecimal encoded data. To import a REG_SZ with this text:

1st Line
2nd Line

You might create a file called MULTILINETEXT.REG that contains this:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Environment]
"MULTILINETEXT"=hex(1):31,00,73,00,74,00,20,00,4c,00,69,00,6e,00,65,00,0d,00,0a,00,\
32,00,6e,00,64,00,20,00,4c,00,69,00,6e,00,65,00,0d,00,0a,00,\
00,00

To encode ASCII into UTF-16LE, simply add a null byte following each ASCII code value. REG_SZ values must terminate with a null character (,00,00) in UTF-16LE notation.

Import the registry change in the batch file REG.EXE IMPORT MULTILINETEXT.REG.

The example uses the Environment key because it is convenient, not because it is particularly useful to add such data to environment variables. One may use RegEdit to verify that the imported REG_SZ data contains the CRLF characters.

Share:
14,794
Aleadam
Author by

Aleadam

Always learning

Updated on June 16, 2022

Comments

  • Aleadam
    Aleadam almost 2 years

    As part of a build setup on a windows machine I need to add a registry entry and I'd like to do it from a simple batch file.

    The entry is for a third party app so the format is fixed.

    The entry takes the form of a REG_SZ string but needs to contain newlines ie. 0xOA characters as separators.

    I've hit a few problems.

    First attempt used regedit to load a generated .reg file. This failed as it did not seem to like either either long strings or strings with newlines. I discovered that export works fine import fails. I was able to test export as the third party app adds similar entries directly through the win32 api.

    Second attempt used the command REG ADD but I can't find anyway to add the newline characters everything I try just ends up with a literal string being added.

  • Aleadam
    Aleadam over 15 years
    Thanks but I'm fine with doing this from a program or even other scripting languages I was just hoping to do it without adding any more dependencies or complication
  • Aleadam
    Aleadam over 15 years
    The vbscript idea solved my problem. The script engine is standard on the windows xp platform I need to run on. To add new lines (0x0a) in vbscripts I used & vbLf
  • Aleadam
    Aleadam over 11 years
    Thanks for the new answer. I'll try that out next time I have a similar issue.
  • CLS
    CLS over 3 years
    This looks terrible, logical, and doable az the same time.