0

I'm creating a .bat script that sets a variable using setx based on dynamic content. I would like to echo back to the user what the new value has been set to. I know from the documentation (https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/setx) that the variable is not available in the current window but will be available in newly opened windows. Is there any way to confirm what the value has been set to and echo it back to the user (without opening a new window)?

Example is shown below.

enter image description here

3
  • 2
    After the setx, on the next line, just add set "fou=This is Foo" Commented Jun 13, 2021 at 11:52
  • Ideally, I'd like to be able to read some source of truth rather than rely on assigning the same value to a different local variable and echo the local variable. Commented Jun 13, 2021 at 12:08
  • 1
    sorry for the different name, just typo error. You have the value you want to set, then you store it in registry via setx and store it in environment variable via set command. Just as simple as it. Or do it backwards, store the value in environment variable and then use that variable in the setx command Commented Jun 13, 2021 at 13:21

1 Answer 1

4

When you use setx, the information is added directly to the registry, so just check the registry key.

In your case you were defining the content for the User Environment, which is located under HKEY_CURRENT_USER\Environment:

%SystemRoot%\System32\setx.exe foo "\"This is foo\""
For /F "Tokens=2*" %G In ('%SystemRoot%\System32\reg.exe Query "HKCU\Environment" /V "foo"') Do @Echo=%H

Example in use: enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Sounds like exactly what I'm looking for. I'll give it a try and let you know how I make out. Thanks ! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.