1

I assume this isn't possible, but when developing software getting up-to-date environment variables is a major pain.

As an example, I have a psake script which updates an env var my application requires:

[System.Environment]::SetEnvironmentVariable($myConfig, $myValue, 'Process')
[System.Environment]::SetEnvironmentVariable($myConfig, $myValue, [System.EnvironmentVariableTarget]::Machine)

This env var is something which may change on a daily/weekly basis as it's based on spun up Azure infrastructure.

In Visual Studio, my app relies on this env var for configuration, and it will not run successfully until I exit VS and reopen it again (so that the 'Machine' set env var is now up to date).

Is there anything which can be done to prevent us having to close and reopen everything again everytime an env var changes on Windows?

3
  • It's any app (including a powershell window), as they all need to unload in order to see the env var change Commented Sep 30, 2020 at 11:32
  • Can you tell us more about the application? How is it deployed (Azure Function, Web App, Worker, API?), which components are involved, where is the env var value coming from? Commented Sep 30, 2020 at 12:31
  • I posted the answer refreshenv which works quite well, but appearantly it's an commando that comes with Chocolatey. Commented Sep 30, 2020 at 12:39

1 Answer 1

1

The current Environment Variables can be read from the registry,

Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name myConfig

As a workaround, you could read the value from the registry and set the Environment Variable again to update your session without the need to reopen.

$myConfig = "myConfig"
$reg = Get-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment' -Name myConfig
$myValue = $reg.myConfig
[System.Environment]::SetEnvironmentVariable($myConfig, $myValue, 'Process')
[System.Environment]::SetEnvironmentVariable($myConfig, $myValue, [System.EnvironmentVariableTarget]::Machine)
Sign up to request clarification or add additional context in comments.

Comments

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.