20
  • Windows 7.
  • It's for my own machine, so it doesn't matter if it requires admin rights or something.
  • Preferably in Python or .NET, but I can learn a bit of Win32 (C/C++) programming if it's necessary.
Javier
  • 4,552
  • 7
  • 36
  • 46

6 Answers6

21

if you want to permanently set environment variable, you can insert the new value into registry. eg with vbscript, add the path "c:\test" into PATH variable

Set WshShell = WScript.CreateObject("WScript.Shell")
strReg = "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path"
strSetting = WshShell.RegRead(strReg)
strNewSetting = strSetting&";c\test"
WshShell.RegWrite strReg, strNewSetting

So, if you use Python or other languages, you can do the same thing using your language's own api/modules to read and write registry

ghostdog74
  • 327,991
  • 56
  • 259
  • 343
11

or you could try a Windows PowerShell script; PowerShell is installed on Windows 7 by default.

run powershell.exe

PS C:\> [Environment]::SetEnvironmentVariable("TestVariable", "Test value.", "User")

Then, for example, from cmd.exe

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\>echo %TestVariable%
Test value.

C:\>

Or (in a new) powershell.exe

PS C:\> echo $ENV:TestVariable
Test Value.
PS C:\>

check out http://technet.microsoft.com/en-us/library/ff730964.aspx

MrDrews
  • 2,139
  • 2
  • 22
  • 22
  • This does work as described, but a DOS shell that's already open when the env var is set does not seem to pick up the change, so apparently, the PowerShell does not broadcast a `WM_SETTINGCHANGE` message as described [here](http://msdn.microsoft.com/en-us/library/windows/desktop/ms725497(v=vs.85).aspx). Good start though. – ssc Apr 04 '14 at 10:28
  • Just if someone might come across this comment: environment variables in a Windows command shell reflect the state of time, when the command shell was opened. Variables defined after that time will not appear in already open command shells. This is a feature (= on purpose), which allows to run different command shells with different environment variables. The same applies e.g. to PowerShell, Linux Bash, etc. – Harry Developer Nov 25 '22 at 09:23
10

In C# the following creates a permanent environment variable:

Environment.SetEnvironmentVariable("foo", "bar", EnvironmentVariableTarget.Machine);
kicsit
  • 638
  • 5
  • 6
  • 1
    It depends on the value of the EnvironmentVariableTarget enumeration used. See http://msdn.microsoft.com/en-us/library/system.environmentvariabletarget.aspx – kicsit Jun 19 '10 at 05:57
  • my debugging window hangs when I add in "EnvironmentVariableTarget.Machine" – Tom Stickel Jun 11 '13 at 00:41
  • Your application (exe or visual studio) should run "as administrator", otherwise you'll have an error "System.Security.SecurityException : Requested registry access is not allowed." – Mr.B Jul 06 '22 at 22:09
6

For anyone else looking for a quick commandline answer

SETX is available on windows servers (natively i think - http://technet.microsoft.com/en-us/library/cc755104.aspx )

Its also available in the Windows 7 and 8 toolkit.

Jeff Martin
  • 10,812
  • 7
  • 48
  • 74
  • 1
    +1 Simpler then previous answer and works on win7 ultimate too – daitangio Jul 02 '14 at 07:33
  • 1
    setx.exe could not be a worse tool for extending `Path`. The user and system `Path` values should never be conflated, and percent variables in a `REG_EXPAND_SZ` value should be left unexpanded. setx.exe can't do any of this. What it can do is make a horrible mess that can't be undone. – Eryk Sun Sep 19 '16 at 12:49
  • don't blame us for wanting to use the command line provided by microsoft: Description: Creates or modifies environment variables in the user or system environment. Can set variables based on arguments, regkeys or file input. – Laurent Picquet Jun 06 '17 at 08:33
2

Use the Environment class like this:

Environment.SetEnvironmentVariable("foo", "bar");
will
  • 3,975
  • 6
  • 33
  • 48
  • 1
    I forgot to mention, is that just for the duration of the program or is it permanent? – Javier Jan 23 '10 at 02:20
  • 1
    It will affect the current program environment only. System wide changes are in the registry (see ghostdog's answer) and require the program to be relaunched after making the change. – Joshua Jan 23 '10 at 02:33
-3

Programmatically modifying environment variables is only for the duration of the program. Have not heard of actually modifying the environment system-wide and making it effective there and then. I do not think that can be done, that would require poking around at privileged level and possibly messing with the core system to achieve that.

Even under Unix, it cannot be done despite some hacks to achieve it. I do remember seeing code that actually did modify the environment variables under MSDOS, by altering the MSDOS's _psp environment data structure, but that was a single-tasking system and 16bit with no protection whatsoever.

To sum up, I do not think you can and it would be unwise to do so, it could be perceived as if the system is under a threat by a 'trojan' or a 'virus' as a result if attempting to do so, not alone that, as a user, I would not like for a program to modify the system environment variable without my consent! Sure, a program can write to the registry to make it permanent, but I would still like to know what is the purpose of it and why.

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • 1
    Have a look at [this](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=vs.85).aspx) for a method set environment variable that stick. -1 for FUD. – ssc Apr 04 '14 at 10:30