How to update environment variable on the same session (immediately) on powershell? - windows

I would like to write some scripts on powershell, it involve using the environment variable on windows
here is the example of the script
test.ps1
setX number 456
echo $env:number
I found that $env:number cannot be updated immedately on the same session of powershell prompt. I need to reopen the powershell prompt. However, this would break my scripts
How can i update the env variable immedately? In linux it is easy to do with EXPORT command, but for windows, it is a hazard...

In PowerSell environment variables are available through a provider. A provider is a way to manupulation all kind of tree containers. have à look at :
Get-PSProvider
Then drives are entities using these providers. have a look at
Get-PSDrive
You can see that it exists a drive called env
You can try :
Get-childItem env:
to set an environment variables you can write :
$env:mavar = "TESTJPB"
To create more permanent environment variables (i.e., user-level or machine-level) you need to use the .NET Framework and the SetEnvironmentVariable method. For example, this command creates a user-level environment variable named TestVariable:
[Environment]::SetEnvironmentVariable("mavar", "TESTJPB", "User")
Have a look to this Microsoft article.

Have you tried
[environment]::setenvironmentvariable()

Related

PowerShell run via PyCharm doesn't see an update for "Path" environment variable

Here is the essence of my question. I added a new path in "Path" environment variable for me and system. PowerShell and cmd run via Start see new commands both. But PowerShell run via PyCharm does not.
I restarted my PC, went deep into Settings - no result.
If one inputs a command gci env: in PyCharm's local PowerShell, there will been shown a list of environment variables, and my new path is in too. So, it MUST see new commands! This make me bewildered.
Please, help me and your nickname will take place in my heart forever.

Path environment variable while running a Windows service under a user account

I am running a Windows service (I cannot modify) under a given user account. The service does a few things, among them runs a bat script that I should provide.
I discovered that the PATH variable available to the service (as seeen by Sysinternal Process Explorer) is the user PATH environment variable. The default is %USERPROFILE%\AppData\Local\Microsoft\WindowsApps which limits the available command in the script to the built-in cmd commands. I need to have a few more path, as when using cmd where the PATH is the merge of user PATH and system PATH.
Short of changing the user PATH variable and adding part of or all system PATH, is there a way to change user PATH variable that would be seen by the Windows service?
I found a suggestion about editing the registry in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\YourService in order to add a PATH environment variable. While it could be the solution I was looking for, unfortunately it did not work. Any suggestion ?

Reading environmental variables in PowerShell or cmd yield different results

I have been puzzling over an issue with environmental variables in Windows 7.
We have a Jenkins server which cannot find the SSH keys in the %HOME% environmental variable as it wants to access the path:
/c/users/jenkins
However if I use
echo %HOME%
in a normal command prompt window as my Jenkins user then the result is
C:\users\jenkins
However, if I use the environment command in Windows PowerShell I also get
/c/user/jenkins
In the normal GUI accessible from the system properties - advanced tag -> environmental I get the following
C:\users\jenkins
I have tried setting them back, but the issue persists. In so far as Jenkins gets the same output as PowerShell.
How do I set them back? (Not that cmd and the Windows system think that they are set wrong.)
How on earth does accessing the same environmental variable give me different outputs?
Is there a way in which this can occur?
Where are they stored so I can manually edit where they are stored?
As mentioned in a comment by #Luis, the env command is not part of PowerShell. It is likely provided by some other set of utilities that you have installed and is intended to emulate the Linux env command.
To refer to an environment variable in PowerShell, use the $env:<variable-name> syntax or the environment provider - Get-Content env:\<variable-name>.
To set an environment variable, you can use $env:<variable-name> = "<variable-value>"
For more information, try running help about_Environment_Variables in PowerShell.

PowerShell Enviromental Variable not seen?

Set by another Program (started as user) my own Environmental Variable using (kernel32.dll:
SetEnvironmentVariableW("ToTestVar_001", "ToTestVar.001 11:11:54" )
So the program that sets the Environmental Variable ToTestVar_001 can read it but I do In PowerShell (started as the same user)
Get-ChildItem Env:
ToTestVar_001 is not listed how can I create an Environmental Variable that can be read and written by my program and PowerShell?
My next try was to create an Env. Var. by PowerShell: "TestVariable"="test Value" but my program can't find it using kernel32.dll:
string rdBuff = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx":
GetEnvironmentVariableW("TestVariable", rdBuff, StringLen(rdBuff));
Where can I find, read and set the Kernel32.dll-Env.Variables in Win 7 64 bit - is that the problem?
Thanks in advance,
Gooly
This is not so simple.
In the way you are writting your code Get-ChildItem Env: will see the environment var positionned by your EXE file using SetEnvironmentVariableW("ToTestVar_001", "ToTestVar.001 11:11:54" ) only if your process (the EXE file) start PowerShell.exe (allowing his children to inherit his environment vars) after positionning the environment variable. In this case PowerShell will inherit of the environment vars and will see it.
It exists another place to set environment vars, the one in the registry I mean the one you can see using advanced system properties :
The environment variables for all users are all stored under:
HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment
The environment variables for a specific user are all stored under:
HKEY_CURRENT_USER\Environment
But once again it's not so simple, if you change the registry and start a process all is ok, the var you set in registry can be seen in the process. But if the process is started when you add the var in the registry, then the process don't see the new environment var except if it manage a special event broadcast by the system informing that the environment vars have been modified (that is the way Explorer.exe is working).

Changing the PATH from a batch

I'm writing a dos-batch in which I need to change the PATH.
I'm using the SET command.
The batch is run from the command line (cmd.exe).
The problem : the changes are only available for the cmd window, and I soon as this window is closed, the changes are dismissed.
How can I change the PATH from a batch and make sure the change will affect the whole system ?
There is a tool setx.exe provided in the Windows XP Service Pack 2 Support Tools that can be used to permanently change an environment variable from the command line:
setx path "%PATH%;C:\New Folder"
Source: http://vlaurie.com/computers2/Articles/environment.htm
The above link also gives the location of the registry keys that store the system / user environment variables - if you are feeling adventurous you could also try setting those.
User environment variables:
HKEY_CURRENT_USER\Environment
System environment variables:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

Resources