How to show value created using setx in current cmd window?

1.3k Views Asked by At

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

1

There are 1 best solutions below

1
Compo On

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