0

I am inserting a variable string in my PATH variable. I set the variables in following manner:

$var="MyTestPath"
$mypath=[environment]::GetEnvironmentVariable("PATH",[system.environmentvariabletarget]::User) 
[environment]::SetEnvironmentVariable("TEST",$var,[system.environmentvariabletarget]::User)
[environment]::SetEnvironmentVariable("PATH",$mypath+";%TEST%",[system.environmentvariabletarget]::User) 

The above code doesn't work for me. %TEST% variable doesn't expand itself when I check the path in the new shell. It shows new path ending with %TEST%. This has always worked when I set this from GUI or from Windows shell prompt. Why is this behavior different when variables are set from PowerShell? Is this feature removed in PowerShell?

I don't want to do the following, because it will keep adding my variable to path everytime I run the script.

[environment]::SetEnvironmentVariable("PATH",$mypath+";"+$var,[system.environmentvariabletarget]::User) 
Booga Roo
  • 1,665
  • 1
  • 21
  • 30
Alok
  • 3,160
  • 3
  • 28
  • 47

2 Answers2

3

Try change this line:

[environment]::SetEnvironmentVariable("PATH",$mypath+";%TEST%",[system.environmentvariabletarget]::User) 

with:

$test =[environment]::GetEnvironmentVariable("test","user") # this retrieve the rigth variable value
[environment]::SetEnvironmentVariable("PATH", $mypath +";$test",[system.environmentvariabletarget]::User) 

%test% have no meaning in powershell, can't be expandend as in CMD.

$env:test retrive only from system environment variable and not from user

CB.
  • 58,865
  • 9
  • 159
  • 159
  • above added `;%TEST%` at the end of my path and it didn't expand to its value. – Alok Sep 26 '12 at 08:43
  • Thanks @Christian. I mentioned in my question that i don't want to take that approach. Everytime I run the script, this will keep appending previous `PATH` to new `PATH`. (`$mypath` is read from user `path` variable). I want to set `PATH=somepath;%TEST%` just one time and keep changing the `TEST` as and when I need thus changing `PATH` automatically. But since `cmd` is not able to expand `%TEST%` this is not wrokging for me. I hope this is not confusing. – Alok Sep 26 '12 at 10:09
  • @Alok You can check if `path` variable contains `$var` before setting it to `path` variable to avoid repetions each script's execution. This If I understood well your needs. Good luck – CB. Sep 26 '12 at 10:15
  • Its not very simple to remove the $var easily. var consists of atleast 15 different paths. Sometimes I change existing path in var in which case i have to maintain old var and new var which will complicate things. I don't want `powershell` to expand `%TEST%` but i just want to set `path` as `somepath;%TEST%`. For `powershell` this is just a string, but when I open `CMD terminal` i expect this variable to be expanded but it doesn't happen. This is something confusing for me. Why does `cmd` differentiate between `%TEST%` when set from GUI or cmd vs when we set same string from `powershell` – Alok Sep 26 '12 at 11:06
  • I guess this question is more for `windows shell` community rather than for `powershell` community. `powershell` is behaving as expected but `windows shell` is not behaving normally. I am hoping someone has experienced this before. – Alok Sep 26 '12 at 11:17
1

You're wanting to effectively set a registry value (that corresponds to a env var) that uses REG_EXPAND_SZ. See this post for details on how to do that.

Community
  • 1
  • 1
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • Thanks for your reply Keith. I tried to set registry variable using the approach in the link. I set my `TEST` variable as `[Microsoft.Win32.Registry]::SetValue("HKEY_CURRENT_USER\Environment","TEST","MyTestString",[Microsoft.Win32.RegistryValueKind]::ExpandString)` Interestingly, powershell is returning me correct value of this variable, but if i open a new windows shell and look for value of `TEST` using `echo %TEST%`, it is returning an old value. I guess i will have to understand further where windows shell reads the value of `TEST` variable from, when it expands `%TEST%`. – Alok Sep 27 '12 at 00:51