Permanent environment variable change

From MicroFocusInternationalWiki
Revision as of 11:36, 16 November 2005 by Rvanherk (Talk | contribs) (Moved text from the ZENworks mail page)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Permanent environment variable change

I want the changes to be permanent

I am trying to update the Path environment variable on Windows 2000 machines through Zenworks. I want the changes to be permanent. I need to delete one path, and add two more. I have tried making an application object and putting the two paths to be added in the 'Environment variables' section - appending to the 'Path' variable with a ; separator. I checked the Path value, and it had not been updated. Any thoughts on how this can be done? Thanks!

Answer

The Microsoft setx.exe utility, which have from the Windows NT 4.0 resource kit, allows you to change an environment variable from a command line such that it will survive a reboot. The version I have (1.0a 5/31/96) appears to work fine on Windows XP Tablet SP2 and Windows 2000. However, it does appear that it was also part of the Windows 2000 resource kit, and you can download it from http://www.microsoft.com/windows2000/techinfo/reskit/tools/existing/setx-o.asp

After you have the utility, you could have a NAL object execute something like
setx Path %Path%;newpath1;newpath2
to do what you requested.

Another suggestion:

A good solution (which is quick and safe) is to run a VBS script to modify the path in the registry. I suspect that the Windows NT/2000 utilities have been dropped from the Resource Kit because they are no longer needed now that WSH is included with XP. I have been using this for while now and have found that setting the registry is both immediate and is retained. Note that you can choose to either set the User or the System path, depending on the user rights, and also the needs of the application. I would also include logic in the code to audit the path and remove any redundant entries etc. The code would be a little more complex but I think it is worth the effort.
The following sample code shows the synxtax for reading and writing the registry.
' ReadSetPath.vbs
' Abstract: VBSript to read/Set PATH environment(s), both USER and SYSTEM.
' Version : 1.0 - Initial draft.
'-----------------------------------------------------------
Dim objShell, strSystemPath, strUserPath, rCode, strNewPath

Set objShell = CreateObject("wscript.shell")
strSystemPath = objShell.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\Path")
strUserPath = objShell.RegRead("HKCU\Environment\Path")

strNewPath = "C:\Temp"
rCode = objShell.RegWrite("HKCU\Environment\Path", strUserPath & ";" & StrNewPath, "REG_SZ")

wscript.echo "USER path = " & strUserPath & _
"System Path = " & strSystemPath