Provides access to the collection of Windows environment variables.
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.
The following code displays an environment variable.
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")
WScript.Echo WshSysEnv("NUMBER_OF_PROCESSORS")
var WshShell = WScript.CreateObject("WScript.Shell"); var WshSysEnv = WshShell.Environment("SYSTEM"); WScript.Echo(WshSysEnv("NUMBER_OF_PROCESSORS"));
Reference
More info on Environment Variables:
Sourced from here: http://technet.microsoft.com/en-us/library/ee156595.aspx
From what I'm reading, it looks like there are four types (or environments) for
environment variables: User, System, Volatile, and Process.
This method doesn’t actually access environment variables directly, but instead it creates an object corresponding to a particular location where the environment variables are stored. In the given example, they created an object that accesses the system environment variables. (the 2nd line of code)
Once you create an object that can access the environment, you then have to actually query the environment for the variable that you are looking for. (the third line of code)
The following are the locations queried for each of the four types:
- System: HKLM\System\CurrentControlSet\Control\Session Manager\Environment
- User: HKCU\Environment
- Volatile: HKCU\VolatileEnvironment
- Process: Not stored in registry.
Unfortunately, the TechNet article is somewhat vague on what exactly the “Process” environment variables are, but I’m guessing that they are stored in a process’ working memory and copied to child processes as they are spawned. Presumably, the shell process (which would be the root for most other processes) loads these values from the registry during logon.
In short, the “Process” environment would appear to cover pretty much everything.
With that said, however, these are the environment variables available in each location (except process, which, again, seems to have everything):
-
System
- COMSPEC
- NUMBER_OF_PROCESSORS
- PATH
- PATHEXT
- PROCESSOR_ARCHITECTURE
- PROCESSOR_IDENTIFIER
- PROCESSOR_LEVEL
- PROCESSOR_REVISION
- OS
- WINDIR
-
User
- PATH
- TEMP
- TMP
- Volatile
- APPDATA
- HOMEDRIVE
- HOMEPATH
- LOCALAPPDATA
- LOGONSERVER
- USERDOMAIN
- USERNAME
- USERPROFILE
Hope that helps!
I just wondered why I can't reach all variables like you - the point is, I suppose, that you can access only _system_ variables, see list:
right click on My Computer, then Properties, then Advanced, then Environment Variables and check list in "System variables" area.
Hope this helped
(I would love to access other variables (like USERNAME, COMPUTERNAME too).
Sincerely,
O.
Addendum: I have found that all my examples work if WshShell.Environment("Process") is used instead. This also retrieves the NUMBER_OF_PROCESSORS environment variable. A better explanation of the argument to WshShell.Envronment is needed.