0

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.

Mark Arnott
  • 992
  • 5
  • 10
  • 18

3 Answers3

3

Powershell is a solid bet here.

$AppendValue="\Homes"
$RegRoot=Get-ItemProperty "hklm:\software\microsoft\windows\currentversion"
$RegValue=$RegRoot.CommonFilesDir+$AppendValue
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.

sysadmin1138
  • 133,124
  • 18
  • 176
  • 300
  • +1 So much cleaner than my answer, which I was forced to delete. :) – jscott Dec 02 '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 02 '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 02 '10 at 22:08
1

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
user9517
  • 115,471
  • 20
  • 215
  • 297
0

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

@echo off  
setlocal

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

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

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

endlocal
MattB
  • 11,194
  • 1
  • 30
  • 36
Simon Catlin
  • 5,232
  • 3
  • 17
  • 20
  • In [Markdown](http://en.wikipedia.org/wiki/Markdown), which SF (partially) uses for formatting, four space characters, rather than back ticks, allows for multi-line code formatting. – jscott Dec 02 '10 at 21:44