Setting Windows environment variable

P

Pavel Ledin

How can I set environment variable for windows(not for currect process)?
What command should I use?
I need functionality like setx.exe
For example I need get env variable, edit it and set it back.

ruby 1.8.5 win
 
J

Jan Svitok

How can I set environment variable for windows(not for currect process)?
What command should I use?
I need functionality like setx.exe
For example I need get env variable, edit it and set it back.

ruby 1.8.5 win

Look for the older thread "Setting Windows Environment Variables".
You'll probably have to mess with registry (see the alst post in the
thread)

J.
 
P

Pavel Ledin

Jan said:
Look for the older thread "Setting Windows Environment Variables".
You'll probably have to mess with registry (see the alst post in the
thread)

Thanks, this works for me:

require 'win32ole'

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 = "<SYSTEM>"
env_var.VariableValue = value
env_var.Put_
end

var_name = 'ABC'
value = 'SOME_VALUE'

create_environment_variable(var_name, value)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top