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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
NVITEnd-user supportCommented:
For USERNAME, try %%USERNAME%%
0
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
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 7

From novice to tech pro — start learning today.