how do I retrieve a value after using setx?
from the doc (setx /?
)
Because SETX writes variables to the master environment in the registry, edits will only take effect when a new command window is opened - they do not affect the current CMD or PowerShell session.
After using setx, you don't need to wait to reboot or get in a new instance/session to be able to get/use this value. this value can be read in Windows Register:
In your case:
setx TIME_VAR %time%
for /f "tokens=3 delims=^ " %%i in ('reg query HKCU\Environment ^| findstr /i /c:"TIME_VAR"') do echo/%%i
setx TIME_VAR %time%
for /f "tokens=3 delims=^ " %%i in ('reg query HKCU\Environment ^| findstr /i /c:"TIME_VAR"') do (
echo/ local and next session = = = =
echo/ Setx Reg Value = "%%i"
echo/ local and next session = = = =
echo/ TIME_VAR Value = %%i
:eof
result:
:: local and next session = = = =
Setx Reg Value = 13:32:05,15
:: in same session and also in next
TIME_VAR Value = 13:32:05,15