Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

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

share|improve this answer
    
As tony has suggested, add it to the user or system environment variables in the advanced system properties. –  joeqwerty Jul 16 '10 at 17:38
    
Sorry - didn't make it clear in the question: This needs to be done programatically as I need to roll out the variable to multiple laptops. –  Ben Jul 16 '10 at 19:58
    
@ben see the updated answer for more details –  tony roth Jul 16 '10 at 21:24
    
use @shay's method! –  tony roth Jul 19 '10 at 20:09
add comment

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")
share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.