0

I am trying to set a custom environment variable in Windows 7, which needs to be done programmatically as it must be set on multiple computers.

I have been testing one a single machine, but When I create it programmatically, it exists for that session (i.e. in DOS when the command window is open, or in Powershell when the ISE is open) - however, when I close the session the variable disappears?

How do I make it permanent?

Thanks,

Ben

Ben
  • 1,137
  • 9
  • 26
  • 44

2 Answers2

3

for users
wmic environment create name='test', variablevalue='air',username='domain\username'

for computer
wmic environment create name='test', variablevalue='air',username='system'

can work remotely by using the /node: paramter

maybe overkill but

http://blogs.technet.com/b/heyscriptingguy/archive/2010/06/03/hey-scripting-guy-can-i-use-windows-powershell-to-read-a-text-file-and-update-an-environment-variable-on-remote-computers.aspx

tony roth
  • 3,884
  • 18
  • 14
1

The basic syntax is:

[Environment]::SetEnvironmentVariable(<name>,<value>,<EnvironmentVariableTarget>)

This will create a new permanant env variable for the logged on user:

[Environment]::SetEnvironmentVariable("NewVar","NewValue","User")

And this one for the system:

[Environment]::SetEnvironmentVariable("NewVar","NewValue","Machine")
Shay Levy
  • 969
  • 1
  • 5
  • 5
  • This needs a reboot to become "visible", right? Is there a way for it to become available instantly? – Ben Jul 20 '10 at 11:33
  • Basically yes, if you want it to be visible to a certain application then just restart that app. – Shay Levy Jul 21 '10 at 14:57