I have an environment variable set via "system properties -> advanced -> Environment Variables". I modified the variable's value. In cmd, I see the new value. In PowerShell, the value is still the old value. Trying to set it with [Environment]::SetEnvironmentVariable doesn't have any effect.
2 Answers
PowerShell will cache its information. Use the Refresh method to solve your problem. I found this in the SQL area too.
-
-
On a normal directory, you should be able to do:
(get-item .).Refresh()
- but that doesn't seem to be available for the Env: folder. Starting a new instance of PowerShell will demonstrate that it's a caching issue - I'm just not sure right now where the Refresh method should live for it. I can query an environment variable called blah usingGet-WmiObject Win32_Environment | where-object -filter {$_.Name -eq 'blah'} | select VariableValue
, and this happily shows me the latest version, even though the cache still hasn't been refreshed for the env: drive. I'll look some more... Commented Jan 18, 2010 at 0:46
Did you launch a new instance of PowerShell?
Every process gets its own environment block when it starts. Typically it gets a copy of the parent process environment block, but CreateProcess can also take a custom one.
This means that changes to the system environment won't affect running processes. So you'll need to start a new one.