0
[Enviroment]::SetEnviromentVariable("Path", "d:/test/bin;c:/test", "Machine"

If the variable already exists with other values in it; how can I adjust this script to not overwrite existing entries. While adding the new additional values?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
John Doe
  • 13
  • 3

1 Answers1

0

When i need to add software to env path using powershell, i use this:

[Environment]::SetEnvironmentVariable("Path", [Environment]::GetEnvironmentVariable("Path", "Machine")+";<YOUR_PATH_HERE>", [System.EnvironmentVariableTarget]::Machine)

But i dont know if efficient, pretty sure a better way exist.

  • This will duplicate the data if it's already there. – Joel Coehoorn Dec 13 '22 at 20:01
  • Yes before running this command, in my script i test if my path are in the path environment variable. But, maybe i'm wrong, but the initial question are "How to add a value to existing system variable", and this command, create this behavior. If you have a better answer, don't hesitate to share it, i would be interested. – OlivierArgentieri Dec 13 '22 at 21:49