Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Refresh $env:path in Powershell?

I have a PowerShell script that checks that a certain directory is on the PATH (by looking through $env:path). It appears that $env:path is loaded and locally scoped by each application on startup, and that scope is passed on to any child applications. So... if someone opens Firefox, downloads my program, runs it, gets a message that they should change their path, fixes the problem, then runs the program again from the Firefox downloads window, they'll get the same message, unless they start my program from Explorer or restart Firefox.

Is there a way to reload $env:path in my PowerShell script so it'll get the current value, as if it were opened from Explorer?

like image 597
Eric W Avatar asked Sep 23 '09 13:09

Eric W


People also ask

How do you refresh an environment variable?

2. Refresh Environment Variables via Command Prompt (CMD) Step 1: In the Start menu, search for Command Prompt and run it as an administrator. Step 2: Type the command: “set PATH = c” (without quotation marks), press the enter key, and restart the Command Prompt.

What is env path in PowerShell?

Published: 18 Oct 2019. The Windows PATH environment variable is where applications look for executables -- meaning it can make or break a system or utility installation. Admins can use PowerShell to manage the PATH variable -- a process that entails string manipulation. To access the PATH variable, use: $env:Path.

How do I get the PATH environment variable in PowerShell?

Environment variables in PowerShell are stored as PS drive (Env: ). To retrieve all the environment variables stored in the OS you can use the below command. You can also use dir env: command to retrieve all environment variables and values.

What does Refreshenv do in PowerShell?

refreshenv (an alias for Update-SessionEnvironment ) is generally the right command to use to update the current session with environment-variable changes after a choco install ... command.


1 Answers

If you were running outside the context of a browser I would tell you to use

[System.Environment]::SetEnvironmentVariable(string name, string value, EnvironmentVariableTarget target) 

to change the Path variable for the user. That third parameter allows you to specify Process, User or Machine. If you specify either User or Machine the change is permanent and will appear in the env blocks of all programs that start after that. However, since you are running within the browser I don't think you would be able to do that.

If the user changes their path, that change will be available to future instances of the browser. Another option is to test (Get-Command) for the app you need in the path and if you can't find it, modify $env:Path yourself in the script each time it runs. That is, unless you don't know what the path should be.

like image 103
Keith Hill Avatar answered Oct 15 '22 06:10

Keith Hill