Reading environmental variables in PowerShell or cmd yield different results - windows

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.

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.

Setting default path with custom variable in windows 10pro

I have been trying to get windows to recognize shortcuts for developer tools. Things like adb for C:\Users\myusername\Andriod\platform-tools\adb.exe. I have tried using CMD and Powershell but they both don't add the PATH I tried the GUI and it doesn't show up.
I've tried setx path "%PATH%;C:\path\to\C:\Users\myusername\Andriod\platform-tools\adb.exe" in powershell and cmd then restarted powershell or cmd
input the variable adb but it pulls a command not recognized error.
Did you try the following?
Adding the path "C:\Users\myusername\Andriod\platform-tools" to your system/user PATH variable? You can do this by start-> environment variable -> environment variable and under system or user, edit the PATH variable and add the above link. Once you do this, restart CMD for it to work.
Add the executable in one of the already existing locations that are in the PATH variable. (Although this is one method, I would not suggest this). Again, if the executable has any dependencies, it must be in a place where the exe itself can access.

Setting environment changes?

I would like to ask you if it's normal that my "Setting environment" changes after each system reboot or even if I just close my cmd console.
for example, I'm using python 3.5, when I wan to use python or to uses pip under the cmd I got an error like :
C:\>python
'python' is not recognized as an internal or external command,
operable program or batch file.
To fix this, I use :
set PATH=%PATH%;C:\Python35
It works after, but as I said before, once I restart my computer, or I close the console, I've the same problem !!!
Thanks in advance for your help and comments. I just would like to inform you that I'm using Windows 7 - 64bits.
You need to add this path in System (Windows+Pause key), Advanced, Environment variables. There you have two sections, System and User, in System, edit the PATH key.
The next time you start a console the path will be present.
Alternaly, if you don't want to modify the setting there (or you have no rights) you could start the app with a batch file which sets the path before launching the app.
In Ruby you can adapt the environment variables from within the script itself by using ENV eg ENV['path'] += 'C:\\Python35'
, I'm sure Python can do this too but wouldn't know how. In your use case this won't help of course.
The SETX command will set the variable permanently. Use SETX /? for information. Set a persistent environment variable from cmd.exe
For Python, many developers use venv which is included with the Python install. https://docs.python.org/3/library/venv.html?highlight=venv#module-venv

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%

Resources