Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I retrieve a value after using setx?

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?

like image 937
code_fodder Avatar asked Jun 05 '14 11:06

code_fodder


People also ask

How do I undo Setx?

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.

Is Setx permanent?

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.

How do I read an environment variable in PowerShell?

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.

How can I see environment variables in CMD?

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


1 Answers

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.

like image 116
Loïc MICHEL Avatar answered Sep 28 '22 09:09

Loïc MICHEL