2

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?

1 Answer 1

3

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%
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.