WshEnvironment Object

Switch View :
ScriptFree
Windows Script Host
WshEnvironment Object

Provides access to the collection of Windows environment variables.

Remarks

Wsh Environment Object graphic

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.

VBScript
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshSysEnv = WshShell.Environment("SYSTEM")
WScript.Echo WshSysEnv("NUMBER_OF_PROCESSORS")
JScript
var WshShell = WScript.CreateObject("WScript.Shell");
var WshSysEnv = WshShell.Environment("SYSTEM");
WScript.Echo(WshSysEnv("NUMBER_OF_PROCESSORS"));
Properties

Item Property | Length Property (WshEnvironment object)

Methods

Count Method | Remove Method

See Also

Reference

Community Content

Tim Lovell-Smith
Sean - Thank you!
This article really made no sense until I read your comment!

Sean.McKay
More Info

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!


just-a-lurker
Maybe it will help a little
Hello,
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.

leftwing
Doesn't work for many environment variables
The example for NUMBER_OF_PROCESSORS works but I find many examples of the variables in my "set" list that do not work.  For example ProgramFiles, SystemDrive, HOMEDRIVE, HOMEPATH all return nothing.  XP/SP3 system.

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.