I need to store a value (a time stamp) and retrieve it later in batch files. So I have searched SO for answers on how to store a persistent variable and found setx.
I used it like this:
C:\tmp>setx TIME_VAR %time%
SUCCESS: Specified value was saved.
But when I try to print it with echo it is not there:
C:\tmp>echo TIME_VAR
TIME_VAR
C:\tmp>echo %TIME_VAR%
%TIME_VAR%
How do I retrieve my stored value?
To unset an environment variable from Command Prompt, type the command setx variable_name “”. For example, we typed setx TEST “” and this environment variable now had an empty value.
The Setx command also retrieves the values of registry keys and writes them to text files. This command provides the only command-line or programmatic way to directly and permanently set system environment values.
Environment variables in PowerShell are stored as PS drive (Env: ). To retrieve all the environment variables stored in the OS you can use the below command. You can also use dir env: command to retrieve all environment variables and values.
To Check if an Environment Variable ExistsSelect Start > All Programs > Accessories > Command Prompt. In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable. For example, to check if NUKE_DISK_CACHE is set, enter echo %NUKE_DISK_CACHE%.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With