views:

11

answers:

1

I need to enumerate the keys in the WScript.Shell.Environment object. It's clear that if you already know the name of the key, you're home free with:

Set oShell = WScript.CreateObject("WScript.Shell")
Debug.WriteLine "PATH=" & oShell.Environment("PATH")

...but if you want to list the keys, it looks like you're out of luck! Is there a secret passageway somewhere?

+3  A: 

The WshEnvironment object is a collection, so you can enumerate it using VBScript'sFor Each ... Next statement:

Set oShell = WScript.CreateObject("WScript.Shell")
Set oEnv = oShell.Environment

For Each strVar in oEnv
  WScript.Echo strVar
Next

The output contains both environment variable names and values, like this:

ComSpec=%SystemRoot%\system32\cmd.exe
NUMBER_OF_PROCESSORS=2
TEMP=%SystemRoot%\TEMP
TMP=%SystemRoot%\TEMP
windir=%SystemRoot%
...
Helen