# Re: Setting Windows Environment Variables

**URL:** <https://www.ruby-forum.com/t/re-setting-windows-environment-variables/64979>\
**Category:** Ruby\
**Created:** [August 15, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/re-setting-windows-environment-variables/64979 "2006-08-15T15:49:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CameronS\_Gemma\_SUK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/45deac/32.png) [@CameronS\_Gemma\_SUK](https://www.ruby-forum.com/u/CameronS_Gemma_SUK)\
**Post date:** [August 15, 2006, 3:49pm UTC](https://www.ruby-forum.com/t/re-setting-windows-environment-variables/64979/1 "2006-08-15T15:49:56Z")

</div>

Hi Austin,

Thanks for all that - but I think you missed the point. I need to set up  
the env path of the machine permanantly. Say we wanted to bring in a new  
build machine or the old one died/I spilled coffee on it… :s The point  
being that I want the environment variables on the machine, not in the  
shortcut/script/kernal I’m running. I have done something similar before  
using NSIS which worked great as lots of programs had to also be  
installed and programs placed in folders etc.

And by thinking in ant - I’m new to ruby and with Ant I know there’s  
pretty much nothing I can’t do - it’s mostly all been tasked before so I  
merely sent a few parameter through and what I want happens magically!

I’m sure it’ll be even easier with Ruby but I’m still finding my feet. (  
:

Thanks a lot for the input everyone, it’s greatly appreciated!  
Especially such great information in such massive quantities and so  
quickly!!!

Regards

Gemma Cameron  
Senior Software Engineer  
Eurofighter ESS (SHM)

tel: 01772 858492

---

<div class="post-metadata">

**Author:** ![CameronS\_Gemma\_SUK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/45deac/32.png) [@CameronS\_Gemma\_SUK](https://www.ruby-forum.com/u/CameronS_Gemma_SUK)\
**Post date:** [August 15, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/re-setting-windows-environment-variables/64979/2 "2006-08-15T18:57:07Z")

</div>

You have to set entries in the Windows registry to do this  
permanently. I’m not in a position at the moment to be able to look up  
the details, but perhaps just knowing this can lead you to finding the  
answer.

Curt

---

<div class="post-metadata">

**Author:** ![CameronS\_Gemma\_SUK](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/45deac/32.png) [@CameronS\_Gemma\_SUK](https://www.ruby-forum.com/u/CameronS_Gemma_SUK)\
**Post date:** [August 15, 2006, 7:12pm UTC](https://www.ruby-forum.com/t/re-setting-windows-environment-variables/64979/3 "2006-08-15T19:12:08Z")

</div>

You can use WMI via win32ole:

require ‘win32ole’

def append\_to\_system\_path(path)  
wmi = WIN32OLE.connect(“winmgmts:\\.\root\cimv2”)  
wql = “Select \* from Win32\_Environment Where Name = ‘PATH’ and  
UserName = ‘’”  
system\_path = wmi.ExecQuery(wql)

system\_path.each do |i|  
i.VariableValue += “;#{path}”  
i.Put\_  
end

end

def create\_environment\_variable(var\_name, value)  
wmi = WIN32OLE.connect(“winmgmts:\\.\root\cimv2”)  
env\_var = wmi.Get(‘Win32\_Environment’).SpawnInstance\_

env\_var.Name = var\_name  
env\_var.UserName = “”  
env\_var.VariableValue = value  
env\_var.Put\_  
end

path = “C:\something\_you\_want\_appended”  
append\_to\_system\_path(path)

var\_name = ‘test’  
value = ‘SOME\_VALUE’

create\_environment\_variable(var\_name, value)
