I'd like to know if there is any way to get all the environment variables and their corresponding value in a computer using VBScript. Apart from the System defined variables, I'd also like to find user defined environment variables. Thanks!
' Remarks ' The WshEnvironment object is a collection of environment variables that is returned by the WshShell object's Environment property. ' This collection contains the entire set of environment variables (those with names and those without). ' To retrieve individual environment variables (and their values) from this collection, use the environment variable name as the index.
' Example ' The following code displays an environment variable.
Set WshShell = WScript.CreateObject("WScript.Shell") Set WshSysEnv = WshShell.Environment("SYSTEM") WScript.Echo WshSysEnv("NUMBER_OF_PROCESSORS")
I understand that for SYSTEM you could use : PROCESS or USER or VOLATILE.
Good luck !
< Message edited by didorno -- 7/20/2005 6:25:51 AM >
Thank didorno. My problem in fact is that I may not know all the environment variable names existed in the computer. Is there any possible way to get all the enviroment variable names, such that I can call the method you suggested to retrieve their respective values?
Sorry Misery, I did read your post not well. I think you can look (or read with a script) in the registry to get the environmental variables. For me (Windows Home XP SP2) the locations are HKEY_CURRENT_USER\ENVIRONMENT HKEY_CURRENT_USER\VOLATILE ENVIRONMENT HKEY_LOCAL_MACHINE\SYSTEM\CURRENTCONTROLSET\CONTROL\SESSION MANAGER\ENVIRONMENT.
For reading the registry you can use the next approach
I get back a full path for those values of environment variables in whichever form, including the one in REG_EXPAND_SZ. I'd like to know if there is any way to change the above code, such that I can get back the short form for REG_EXPAND_SZ type value. Instead of returning C:\Windows, I'd like to get back %SystemRoot%. Is there any ways to do so?
If you modify the next code you can use the strings to obtain your wish, I think.
set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM") WScript.Echo "System" For Each Env in WshSysEnv WScript.Echo Env next Set WshSysEnv = WshShell.Environment("PROCESS") WScript.Echo vbCrLf & "Process" For Each Env in WshSysEnv WScript.Echo Env next Set WshSysEnv = WshShell.Environment("USER") WScript.Echo vbCrLf & "User" For Each Env in WshSysEnv WScript.Echo Env next Set WshSysEnv = WshShell.Environment("VOLATILE") WScript.Echo vbCrLf & "Volatile" For Each Env in WshSysEnv WScript.Echo Env next
Good luck !
< Message edited by didorno -- 7/20/2005 6:27:50 AM >
Thanks again didorno, but my problem is that I'd like to get the environment variables from a network server. If I use the method you suggested, I guess I can only get the values from my computer which runs the script.
I can use the first method you suggested to retrieve all the environment variables from the network server by changing "." to the name of the server. But I cannot get the non-expanded form of the values of environment variables. Do you have any suggestion?
mbouchard, that is what I do most of the time ! Plus a few minor adaptations. In fact, I learn everything from examples and modifying them. And a lot of trial and error....... You know what I mean. I'm thinking about a way to be able to show all possible underlying commands one level lower than f.i.
So, how can I show all WshSysEnv.?????????? properties and methods ?