3

I added an environment variable to my System Path -> C:\Program Files\TEE-CLC. After that, I expect, that I can run my foo.exe which is under C:\Program Files\TEE-CLC from PowerShell without providing a full path to it. The issue is when I start a new, fresh Powershell session under my user, it doesn't see the newly added path, but when I start it under administrator everything works fine.

I checked $env:Path in both modes (User and Admin) and found that my new path doesn't exist when I am under User, but I can see it when Powershell is as Administrator

How I can make my newly added variable visible running under User rather than Administrator?

UPDATE I ran 2 commands under User and the result is different. Why doesn't $env:Path display the same output as [System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE') and why current PSSession doesn't see the path from the latter command?`

$env:Path

STEPS TO REPRODUCE

  1. Add environemt path variable [Environment]::SetEnvironmentVariable('PATH', ($env:Path + ';C:\Program Files\Foo'), 'MACHINE')
  2. Close the session
  3. Open a NEW ps session as user -> check $env:PATH -> C:\Program Files\Foo is not there
  4. Open a NEW ps session as admin -> check $env:PATH -> C:\Program Files\Foo is there
6
  • How did you add "C:\Program Files\FOOPATH" to the PATH variable? SETX? GUI? Commented Oct 16, 2020 at 15:34
  • 1
    @lit It was added from PS script, using [Environment]::SetEnvironmentVariable('PATH', $UpdatedPathList, 'MACHINE') Commented Oct 16, 2020 at 15:36
  • To change the value of Path, you'd use this [Environment]::SetEnvironmentVariable("Path", $Env:Path + ";C:\SomeLocation", "Machine"), so unfortunately as you've not shown or provided the code which specifically defines the value of the $UpdatedPathList variable, we don't know if it contains the required information. Also you need to understand that whilst you may have added it to the System, (Machine), environment, your PS Session inherits only what was defined when it started, new additions are not propagated into an existing session. Commented Oct 16, 2020 at 16:47
  • When you view the Path values, it will show all of those available under Machine, and User, when the PS session was opened, and any created in that session, i.e as Process. If you want to create a persistent variable, in Machine or User, and wish also to use it in the same session, you'd need also to define it as a Process variable e.g. $Env:Path += ";C:\SomeLocation", or [Environment]::SetEnvironmentVariable("Path", $Env:Path + ";C:\SomeLocation", "Process"). Commented Oct 16, 2020 at 17:06
  • 1
    @Compo It is besides a point: 1) I explicitly provided info that I ran The issue is when I start a new, fresh Powershell session 2) Take a look at the picture, the path is there. Therefore, it was provided to the SetEnvironmentVariable function correctly. I think I need to provide more info because apparently, the question is not clear. Get to the STEPS TO REPRODUCE section in order to reproduce the behavior on your machine Commented Oct 16, 2020 at 17:27

2 Answers 2

1

Unless I've missed something, I cannot reproduce your issue, here's example, I've just perfromed on a Test machine for you.

First I opened an elevated powershell.exe window, i.e. 'as administrator`:

Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. Try the new cross-platform PowerShell https://aka.ms/pscore6 PS C:\WINDOWS\system32> $Env:Path C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\Tester\AppData\Local\Microsoft\WindowsApps PS C:\WINDOWS\system32> [Environment]::SetEnvironmentVariable('Path', ($Env:Path + ';C:\Program Files\Foo'), 'MACHINE') PS C:\WINDOWS\system32> $Env:Path C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\Tester\AppData\Local\Microsoft\WindowsApps PS C:\WINDOWS\system32> [Environment]::GetEnvironmentVariable('Path', 'MACHINE') C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\Tester\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Foo PS C:\WINDOWS\system32> 

Then I opened a new non elevated powershell.exe window:

Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. Try the new cross-platform PowerShell https://aka.ms/pscore6 PS C:\Users\Tester> $Env:Path C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\Tester\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Foo;C:\Users\Tester\AppData\Local\Microsoft\WindowsApps; PS C:\Users\Tester> [Environment]::GetEnvironmentVariable('Path', 'MACHINE') C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\Tester\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Foo PS C:\Users\Tester> 

As you can see C:\Program Files\Foo was shown in both example outputs.

Then I opened another new elevated powershell.exe window:

Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. Try the new cross-platform PowerShell https://aka.ms/pscore6 PS C:\WINDOWS\system32> $Env:Path C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\Tester\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Foo; PS C:\WINDOWS\system32> [Environment]::GetEnvironmentVariable('Path', 'MACHINE') C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\Tester\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Foo PS C:\WINDOWS\system32> 

As you can see C:\Program Files\Foo was shown in both example outputs.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! It might be something env dependent. Will try to figure it out
Same happened to me, but it was a normal variable, not the path. Windows 11 24H2. Winblows is getting ridiculous tbh
1

This issue suddenly appeared for me out of the blue and I was able to solve it for myself at least.

Warning: backup your PATH somewhere before running anything. There's a 1024ch limit so be careful.

Background

As a user (non-admin), your $Path is the equivalent of [System.Environment]::GetEnvironmentVariable('PATH', 'USER') while as admin your $Path is the equivalent of [System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE'). There are probably many instances where you want these separated. In my case everything under my "MACHINE" $Path are things I'd want as a regular user, so I'll just show how to combine these paths so that no matter how you run powershell you'll have the same path.

Process

  1. Open cmd.exe as an administrator (there's probably a way to do this in powershell but this method seems so much easier and will apply to powershell fine)
  2. Add the combined paths to the user path:
setx path "%path%" 

(within cmd %path% is a combination of both the user and machine paths)

  1. (Extra) Add the combined paths to the system path:
setx /M path "%path%" 
  1. Restart powershell. The executables you expect should now be available as a user.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.