Using Environment Variable for regedit in batch

Hi experts,

I want to change a registry value using the Netlogon Skript for each user in my domain.
The registry Key looks as follows:

[HKEY_CURRENT_USER\Software\test\3.0]
"disk_status_testuser@test"="test"

Open in new window


I want to change the value for "test_testuser_test2" now to "Testentry2".
The part "testuser" is different for any user. Instead of testuser there must be %USERNAME%, because it is always the current username.

I tried following, but that doesn`t work:

[HKEY_CURRENT_USER\Software\test\3.0]
"disk_status_%USERNAME%@test"="test"

Open in new window


How can I get this done in my Logon-Skripts?
It is also important that the value is "Testentry2" (with the ").

How can this be implemented?

Many thanks in advance!
SystemadministrationAsked:
Who is Participating?
 
oBdACommented:
@echo off
setlocal enabledelayedexpansion
set Key=HKCU\Software\test\3.0
set Value=disk_status_%UserName%@test
set OldData="Test"
set NewData="TestEntry2"
set CurrentData=
for /f "tokens=2*" %%a in ('reg.exe query "%Key%" /v "%Value%" 2^>NUL ^| find /i "%Value%"') do set CurrentData=%%b
if "%CurrentData%"=="" (
	echo Value '%Value%' not found, skipping.
) else (
	if /i "%CurrentData%"=="%OldData%" (
		echo Setting new data '%Data%' ...
		reg.exe add "%Key%" /v "%Value%" /t REG_SZ /d "%Data:"=\"%" /f
	) else (
		echo Kept existing data '%CurrentData%'.
	)
)

Open in new window

0
 
oBdACommented:
Sorry, it's not quite clear what it is you actually want to do.
Do you just want to change "test" to "TestEntry2"? Then the following should work:
reg.exe add "HKCU\Software\test\3.0" /v "disk_status_%UserName%@test" /t REG_SZ /d "TestEntry2" /f

Open in new window

If not, please describe further.
0
 
NVITCommented:
For USERNAME, try %%USERNAME%%
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
SystemadministrationAuthor Commented:
There are the "" missing after changing the Entry.

In Registry there should really be "Testentry2"
What you told produces Testentry2 (without the "")

Many thanks!
0
 
oBdACommented:
Then use this:
reg.exe add "HKCU\Software\test\3.0" /v "disk_status_%UserName%@test" /t REG_SZ /d "\"TestEntry2\"" /f

Open in new window

0
 
SystemadministrationAuthor Commented:
Found it:

reg.exe add "HKCU\Software\test\3.0" /v "disk_status_%UserName%@test" /t REG_SZ /d [b]\[/b]"TestEntry2[b]\[/b]" /f

Open in new window


Backslashes for masking the " is what was missing.
Thanks for your fast reply!
0
 
SystemadministrationAuthor Commented:
Another Question:

Is it possible to edit the value only if it exists?
The code above always adds the new value.
I want to check whether a value is present and if yes, change it.

Thanks in advance!
0
 
SystemadministrationAuthor Commented:
The value currently contains C:\Program Files(x86)\....
If I try the script it tells me:

"Files" can`t be processed at this place.

Do you know where there is the error?
0
 
oBdACommented:
Try this; note that in the NewData variable, round brackets need to be escaped with a "^":
@echo off
setlocal enabledelayedexpansion
set Key=HKCU\Software\test\3.0
set Value=disk_status_%UserName%@test
set OldData="D:\Program Files (x86)\Acme"
set NewData="C:\Program Files ^(x86^)\Acme"
set CurrentData=""
for /f "tokens=2*" %%a in ('reg.exe query "%Key%" /v "%Value%" 2^>NUL ^| findstr.exe /i /r "\<%Value%\>"') do set CurrentData=%%b
if %CurrentData%=="" (
	echo Value '%Value%' not found, skipping.
) else (
	if /i %CurrentData%==%OldData% (
		echo Setting new data '%NewData%' ...
		reg.exe add "%Key%" /v "%Value%" /t REG_SZ /d "%NewData:"=\"%" /f
	) else (
		echo Kept existing data '%CurrentData%'.
	)
)

Open in new window

0
 
LeeTutorretiredCommented:
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
 
oBdACommented:
The original question was actually already answered in http:#a40642327
The additional request (http:#a40648718) to change only if an entry existed was solved in the revised script in http:#a40656907.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.