PowerShell Enviromental Variable not seen? - windows

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

Related

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.

Seting the PATH environment variable via cmd

I tried to add to the PATH environment variable ";C:\my_EXE" so I can run the programs I put there via cmd (windows 8).
I tried this command:
set PATH=%PATH%;C:\my_EXE
but it changed the PATH environment variable only until the CMD window was closed.
I searched on Google and I found this command:
setx PATH "%PATH%;C:\my_EXE"
that is supposed to set it forever, but it also works only until the CMD window has closed
and it made something like this:
[new PATH]=[old PATH]X2
It appears only in a new cmd and not in system properties(!=cmd, there is the PATH with my new path and not X2)
Why does this happen?
How can I set the PATH environment variable without problems?
There are two persistent PATH variables, the per-machine variable and the per-user one. They get appended together to produce the actual environment variable. (Environment variables in the PATH are also expanded at this point.)
That's why you're getting the path doubled up, because you've set the per-user persistent variable to include everything from the environment variable (which already includes everything from the per-machine persistent one).
You can use setx with the /m parameter to set the per-machine persistent variable, but this isn't ideal:
If the per-user persistent variable is set, its contents will be copied into the per-machine persistent variable, which is likely to be inappropriate;
If the persistent variable references other environment variables, the references will be replaced with the current value of those variables. If the referenced variables change, the PATH will no longer follow those changes. (To be honest, though, most of the time this won't matter: the feature isn't commonly used.)
Instead, consider using pathman which is specifically designed to manipulate paths. You can get pathman.exe from the Windows Server 2003 Resource Kit Tools download.
Note that both setx and pathman may hang if there are any unresponsive GUI applications running, even if the application window is hidden. The best way to minimize this risk is to reboot the machine immediately before running any script that uses setx or pathman.
You should use setx command with "/m" parameter.

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

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()

Setting permanently variables in windows %username%

i need to set permanently the %username% variable on windows is that possible?
I mean if I do on command prompt set username=UPPERCASE I can see that the variable is changed, however as expected this does work just in that command prompt, If I open another one the username variable is the original.
I tried to find the correspondent registry value but I did not find it.
I need some automatic way to do it.
Thanks!
When each process starts, the process gets a copy of the parent process' environment variables. So, if you used Windows Explorer to start your command prompt. You get a copy of Explorer.exe's environment variables, but when you edit it in cmd.exe, you don't edit the value for the rest of the system.
That said, Windows provides an event that processes can subscribe to so they can be told that there is a new value for environment variables. If you are interested, I can try to dig it up. I've used it before for the Path environment variable and think it may apply to your problem.
Sounds like you should create a bat file. This will prompt the user for their username and put it in the %USERNAME% environment variable.
SET /p USERNAME=What is your UserName?
putty /user:%username%

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