Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to create a script to run on a server that I do not have access to.

I need the script to append a string to an existing string value (Type = REG_SZ). The script cannot just replace the entire string with a new value as I don't know what is currently in the entry and I can't lose what is already there.

I was thinking along the lines of regini.exe, but i couldn't figure out how to export, append, import with regini.exe and a batch file. Maybe powershell could do the trick.

share|improve this question
up vote 3 down vote accepted

Powershell is a solid bet here.

$RegRoot=Get-ItemProperty "hklm:\software\microsoft\windows\currentversion"
Set-ItemProperty -path HKLM:\software\microsoft\windows\Currentversion -Name CommonFilesDir -Value $RegValue

Windows gets twitchy about anything that touches reg or registry.exe, where scripts like the above can run with less complaining.

share|improve this answer
+1 So much cleaner than my answer, which I was forced to delete. :) – jscott Dec 2 '10 at 21:39
Your second registry path doesn't match your first, so this script is creating a new value with the appended string, but I get the gist. That's slick! – Mark Arnott Dec 2 '10 at 21:56
@Mark Arnott That's what I get for doing it off the top of my head. Glad it worked out! Powershell is very, very nifty. – sysadmin1138 Dec 2 '10 at 22:08

Vbscript will do this too

Set WshShell = WScript.CreateObject("WScript.Shell")
Dim Temp
'For the purpose of demonstration create a new key and give it a default of 1
WshShell.RegWrite "HKCU\MyNewKey\", 1 ,"REG_SZ"
'Add a value 
WshShell.RegWrite "HKCU\MyNewKey\MyValue", "Hello world!"
'read the value we just wrote append more text to it and write it back
Temp = WshShell.RegRead("HKCU\MyNewKey\MyValue")
Temp = Temp & " More Text"
WshShell.RegWrite "HKCU\MyNewKey\MyValue",Temp
share|improve this answer

You can achieve this using PowerShell, or using Windows' REG.EXE, e.g.:

@echo off  

set SERVER=myserver  
set KEY=HKLM\Software\Microsoft\Windows\CurrentVersion\Run  
set VALUE=myvalue  
set APPEND_DATA=my appended text

for /f "tokens=2,*" %%V in ('%SystemRoot%\System32\reg.exe query "\\%SERVER%\%KEY%" /v "%VALUE%"') do set DATA=%%W

set DATA=%DATA:"=\"%  
%SystemRoot%\System32\reg.exe add "\\%SERVER%\%KEY%" /v "%VALUE%" /t REG_SZ /f /d "%DATA%%APPEND_DATA%"  

share|improve this answer
In Markdown, which SF (partially) uses for formatting, four space characters, rather than back ticks, allows for multi-line code formatting. – jscott Dec 2 '10 at 21:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.