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

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);

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.


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.