WSH » Objects » WshEnvironment

The WshEnvironment object is a read/write collection object that contains the environment variables. This object cannot be instantiated directly. It can only be accessed through the WshShell.Environment property.

As with all collection objects, the WshEnvironment collection can be iterated using the VBScript "For Each .. In" statement, but cannot be iterated using the JScript "for .. in" syntax. You can use the following method in JScript to iterate through this collection:

objShell=new ActiveXObject("WScript.Shell")
env=objShell.Environment;

for(e=new Enumerator(env); !e.atEnd(); e.moveNext())
document.write(e.item(e) + "<br>");

When iterated using the VBScript "For Each ... In" syntax, the values assigned to the loop variable are of the form "Name=Value".

Properties

Item

Syntax: object[.Item] (strName)

The Item property is the default property of the WshEnvironment object. It takes a single argument of type String, and returns the value of the environment variable with the specified name, or the empty string if no such environment variable exists in the collection. When used on the left hand side of an assignment operation, this property adds a new environment variable with the specified name, and assigns it the specified value.

length

Syntax: object.Length

The length property is read only and returns a Long value which is the number of items in the collection.

Methods

Count

Syntax: object.Count ()

The Count method returns a Long value which is the number of items in the collection.

Remove

Syntax: object.Remove (strName)

The Remove method deletes the environment variable with the specified name. If no such environment variable exists, a runtime error will be generated.

See Also: