views:

343

answers:

1

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?

+4  A: 

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.

Keith Hill
Nope, that's what I was looking for. [System.Environment]::GetEnvironmentVariable("PATH", "Machine") got me the current setting of the path, regardless of what app my script was run from or when that app was opened. Thanks for the help.
Eric W