2

Is there a way to call the system path if the path has been changed in the current terminal? i.e.:

$env:Path = "C:\some new path"   
#some coding that requires a different path set up
$env:Path = $defaultPath #would have to define $defaultPath by calling the system default path
5
  • 1
    [1] replacing the environment path for a one-off use is ... peculiar & dangerous. do not do that if you can avoid it. [grin] ///// [2] if you need to modify or add to the env-path, then do that, don't REPLACE it with a shortened path. ///// [3] if you MUST replace the env-path with another & need to access the old path later ... save the old path & restore it as needed. ///// [4] a far safer method would be to start a new session, destroy the env-path in that session, work there, and then exit that session ... continuing your work in the original session. Commented Apr 30, 2022 at 20:25
  • That is exactly what this does. It does not change the system path, only the path in that terminal session Commented Apr 30, 2022 at 20:26
  • 2
    @SantiagoSquarzon I just figured out that you can do that. Thanks! Commented Apr 30, 2022 at 20:51
  • @user14894283 - that is not what you implied [grin] you said you wanted to reference the default path after you had replaced it. the only way to do that is to restore it since the apps that use the path are not aware of any other way to do what the OS does with the Path env-var. ///// the only 2 ways to to that are [A] to do things as intended ... add to the path in the current session. [B] swap them back-n-forth as needed. the 2nd of those is not recommended. Commented Apr 30, 2022 at 21:08
  • Thanks. Could you take a look at my if statements? Commented Apr 30, 2022 at 22:11

2 Answers 2

3

Use the following to reload the $env:PATH environment variable from the registry, as future sessions would see it (assuming no further relevant registry updates are made).

If your current session hasn't made any relevant registry updates, this is the same as getting the value that was in effect on session startup - barring any dynamic additions via a $PROFILE script:

$env:PATH = [Environment]::GetEnvironmentVariable('Path', 'Machine'),
            [Environment]::GetEnvironmentVariable('Path', 'User') -join ';'

Note:

  • A process' effective $env:PATH value is a composite value of a machine-level and a user-level registry entry, with the machine-level definition taking precedence, as reflected in the two .NET API calls above.

    • Note that the underlying registry locations - HKEY_LOCAL_MACHIN\System\CurrentControlSet\Control\Session Manager\Environment and HKEY_CURRENT_USER\Environment - are REG_EXPAND_SZ registry values, i.e. they may be defined in terms of other environment variables, such as %SystemRoot% and %ProgramFiles%.

    • Both the .NET API calls above - using [Environment]::GetEnvironmentVariable() - and PowerShell's Get-ItemProperty and Get-ItemPropertyValue cmdlets expand (interpolate) such references and return verbatim paths - which is what new processes see by default too.

  • Given the above, the only way to robustly retrieve the value that was in effect at session startup time is to save it in a variable at startup time.

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

10 Comments

I don't want it to save. It's specific so that I run the command and it changes the path for the current terminal
Your response is beyond my knowledge level. I'll have to do some reading. For the time being, could you take a look at my if statement. I think I need a flag/tracking variable but I dont quite understand those yet
@user14894283, I don't understand what you mean by to save. The call in this answer only retrieves definitions from the registry, in a manner that mimics what new processes will see in $env:PATH and - if no change was made to the relevant registry locations since session startup - what your session originally saw (before potential modification via $PROFILE).
@user14894283, please ask follow-up questions neither (a) via your original question post, nor (b) via answer posts - answer posts should only be used for actual answers to the question at hand. Either use comments to ask for minor clarifications, or create a new question post.
@user14894283: what you used wasn't a comment but an answer post. Please note that, in general, this site isn't a chat forum; instead, its strength comes from focused, well-defined questions and similarly focused answers. That's what provides the most benefit to future readers.
|
2

It's still stored in the registry so you can just query it:

  • Located: HKLM:\System\CurrentControlSet\Control\Session Manager\Environment
$key = "HKCU:\Environment",
       "HKLM:\System\CurrentControlSet\Control\Session Manager\Environment"
       
(Get-ItemPropertyValue -Path $key -Name Path) -Join ';'

Querying the key using Get-ItemPropertyValue (as suggested by Mklement) will give you just the property's value.

3 Comments

Thanks. But what is the value stored as?
Thanks I figured it out, could you help me figure out why my if then statements are not working?
@user14894283, what isn't working?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.