Windows: User environment variable cannot be displayed with echo - environment-variables

I am looking at a user environment variable created using Windows console command
reg add "hkcu\environment" /v SARMaster_Server_Name /d %smServer%
This variable name SARMaster_Server_Name can be seen in the registry under HKCU/Environment with the correct value as well as through the advanced System settings of the Control Panel.
However, when I run
echo %SARMaster_Server_Name%
I get %SARMaster_Server_Name% instead of the actual value of this environment variable. What did I miss? All other environment variables such as TEMP can be echoed.

Variable is being set correctly using
reg add "hkcu\environment" /v SARMaster_Server_Name /d %smServer%
However, I need to restart to be able to access this variable. A workaround, which does not help in my scenario is to open the Environment Variables editor from the Control Panel and click on OK after the variable has been added using "reg add". Somehow, this action registers the new variable which can now be accessed on a new CMD session. Problem is being reported on XP so it may have been fixed on Windows 7 a

Related

Can't find which *FILE* save the environment variable for cshell

When I hit the cmd set in csh it shows me a list of env variables I have right now in my session.
I was wondering where they are set/saved and couldn't find the location.
I have tried in files ~/.cshrc and ~/.cshrc.myusername and in both I saw none of the environment variables that set shows .
Where are they?
In memory. Each instance of csh will get a new copy of your default environment. Setting variables at the command prompt does not persist them anywhere for future sessions.

RDP "ClientName" Environment Variable is null, if run as administrator

We have developed a windows application and deployed in terminal server / citrix environment.
We have used the Enviornment.GetEnvironmentVariable("CLIENTNAME") for getting the client name
from where the RDP is accessed.
If I run the application with normal privilege (double cliking the application), then i am getting
correct value in the "ClientName" Env Variable.
But when I run the same application with administrator privilege (right click and run as administrator),
then then "ClientName" Env Variable returns null.
Note: I wrote a small application and get all the environment variables exists in the virtual machine (RDP)
using "Environment.GetEnvironmentVariables()". "ClientName" Env variables is shown only when it is executed with normal privilege
and the same variable is hidden if executed with administrator privilege.
Can anyone let us know why the "ClientName" Env variable is hidden on administrator privilege?
Regards,
Guru
This sounds like this might be your problem:
When connecting remotely with Remote Desktop Connection, the
environment variables CLIENTNAME and SESSIONNAME are added to each
process that is started.
If you set the Folder Option "Launch folder windows in a separate
process" and later launch an application from an additional Explorer
window, the application will not see these additional environment
variables.
To fix the issue:
If your application relies on these variables, remove the folder
option "Launch folder windows in a separate process".
MS Article: https://support.microsoft.com/en-us/kb/2509192
$sessionID = (Get-Process -PID $pid).SessionID
$PC = (Get-ItemProperty -path ("HKCU:\Volatile Environment\" + $sessionID) -name "CLIENTNAME").CLIENTNAME

PyCharm not updating with environment variables

When I use vim to update my environmental variables (in ~/.bashrc), PyCharm does not get the updates right away. I have to shut down the program, source ~/.bashrc again, and re-open PyCharm.
Is there any way to have PyCharm source the changes automatically (or without shutting down)?
When any process get created it inherit the environment variables from it's parent process (the O.S. itself in your case). if you change the environment variables at the parent level, the child process is not aware of it.
PyCharm allows you to change the environment variables from the Run\Debug Configuration window.
Run > Edit Configurations > Environment Variables ->
In my case pycharm does not take env variables from bashrc even after restarting
Pycharm maintains it's own version of environment variables and those aren't sourced from the shell.
It seems that if pycharm is executed from a virtualenv or the shell containing said variables, it will load with them, however it is not dynamic.
the answer below has a settings.py script for the virtualenv to update and maintain settings. Whether this completely solves your question or not i'm not sure.
Pycharm: set environment variable for run manage.py Task
I recently discovered a workaround in windows. Close Pycharm, copy the command to run Pycharm directly from the shortcut, and rerun it in a new terminal window: cmd, cmder, etc.
C:\
λ "C:\Program Files\JetBrains\PyCharm 2017.2.1\bin\pycharm64.exe"
I know this is very late, but I encountered this issue as well and found the accepted answer tedious as I had a lot of saved configurations already.
The solution that a co-worker told me is to add the environment variables to ~/.profile instead. I then had to restart my linux machine and pycharm picked up the new values. (for OSX, I only needed to source ~/.profile and restart pycharm completely)
One thing to be aware is that another coworker said that pycharm would look at ~/.bash_profile so if you have that file, then you need the environment variables added there
In case you are using the "sudo python" technique, be aware that it does not by default convey the environment variables.
To correctly pass on the environment variables defined in the PyCharm launch configuration, use the -E switch:
sudo -E /path/to/python/executable "$#"
This is simply how environment variables work. If you change them you have to re-source your .bashrc (or whatever file the environment variables are located in).
from dotenv import load_dotenv
load_dotenv(override=True)
Python-dotenv can interpolate variables using POSIX variable expansion.
With load_dotenv(override=True) or dotenv_values(), the value of a variable is the first of the values defined in the following list:
Value of that variable in the .env file.
Value of that variable in the environment.
Default value, if provided.
Empty string.
With load_dotenv(override=False), the value of a variable is the first of the values defined in the following list:
Value of that variable in the environment
Value of that variable in the .env file.
Default value, if provided.
Empty string.

Ubuntu: Environment variable is deleted after closing session

I am setting an environment variable in Ubuntu 14.04 for a script to use it.
I opened the terminal and did:
export VARNAME=/home/me/folder/folder2
And then run the script and everything works fine. But anyway as soon as I close my session, the variable seems to disappear and I have to declare it again like the first time.
To set an environment variable that doesn't get erased with the closing of the terminal (Ubuntu 16.04), follow these steps:
Open .bashrc file by using the text editor of your choice. For me, it was
code ~/.bashrc as I use VS Code, but you can use vi ~/.bashrc or subl ~/.bashrc.
Add the environment variable using export VARNAME=/home/me/folder/folder2
Save the file and close.
The variable will persist even after the terminal is closed.
Actually if you set the variable via terminal it will last till shutdown. If you want to set permanent variable you have to do the following.
$ vi ~/.bash_proflle
// set the variable in the file
exit by pressing esc key and type :wq Now the path is set.

Use Environment Variable in the same process that assigned it

I have an Installer that assigns an Environment variable using setx command
Afterwards, that installer invokes a command line that uses this enviroment variable but in that context the variable is still empty.
If I invoke the command line independently the variable is read properly.
Why is that? and how can I overcome that?
I've experimented extensively with SETX. Variables set via SETX cannot be seen in the process or script that sets them, unless you programmatically re-read the pertinent Registry key.

Resources