23

Is is possible to read system environment variables in a Windows Scripting Host (WSH) VBS script?

(I am writing a VBScript using Windows Scripting Host for task for a Cruise Control and want to pick up the project build URL.)

mike nelson
  • 21,218
  • 14
  • 66
  • 75

5 Answers5

31

Here's an example (taken from here):

Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
comp=oShell.ExpandEnvironmentStrings("%ComputerName%")
WScript.Echo user & " " & comp
M4N
  • 94,805
  • 45
  • 217
  • 260
10

The existing answers are all helpful, but let me attempt a pragmatic summary:

Typically, you want the current process's definition of an environment variable:

CreateObject("WScript.Shell").ExpandEnvironmentStrings("%TEMP%")

This is the equivalent of (note the absence of % around the variable name):

CreateObject("WScript.Shell").Environment("Process").Item("TEMP")

Caveat: Do not omit the ("Process) part: if you do, you'll get the system scope's definition of the variable; see below.

.ExpandEnvironmentStrings is conceptually simpler and more flexible: It can expand arbitrary strings with embedded (%-enclosed) environment-variable references; e.g.:

CreateObject("WScript.Shell").ExpandEnvironmentStrings("My name is %USERNAME%")

On rare occasions you may have to access environment-variable definitions from a specific scope (other than the current process's).

  sScope = "System" ' May be: "Process", "User", "Volatile", "System"
  CreateObject("WScript.Shell").Environment(sScope).Item("TEMP")

Note: As stated above, omitting the scope argument defaults to the System scope.

Caveat: Accessing a value this way does not expand it: Environment-variable values can be nested: they can refer to other environment variables.
In the example above, the return value is %SystemRoot%\TEMP, which contains the unexpanded reference to %SystemRoot%.
To expand the result, pass it to .ExpandEnvironmentStrings(), as demonstrated above.

mklement0
  • 382,024
  • 64
  • 607
  • 775
7

From here ...

Set WshShell = WScript.CreateObject("WScript.Shell")

Set WshProccessEnv = WshShell.Environment("Process")
Set WshSysEnv = WshShell.Environment("System")

Wscript.Echo WshSysEnv("NUMBER_OF_PROCESSORS")
Wscript.Echo WshProccessEnv("Path")

Also, much more detail on TechNet.

mklement0
  • 382,024
  • 64
  • 607
  • 775
JP Alioto
  • 44,864
  • 6
  • 88
  • 112
6
Set WshShell = CreateObject("WScript.Shell")    
Set WshEnv = WshShell.Environment
WScript.Echo "WINDIR=" & WshEnv.Item("WINDIR")  & vbCrLf & vbCrLf   
Set WshShell = CreateObject("WScript.Shell")
WScript.Echo "Environment System:"              & vbCrLf & _ 
         "..............................................."

For Each IEnv In WshShell.Environment("System")
    WScript.Echo IEnv
Next

WScript.Echo vbCrLf & "Environment User:"       & vbCrLf & _   
        "..............................................."

For Each IEnv In WshShell.Environment("User") 
    WScript.Echo IEnv
Next

WScript.Echo vbCrLf & "Environment Volatile:"   & vbCrLf & _ 
       "..............................................."

For Each IEnv In WshShell.Environment("Volatile")
    WScript.Echo IEnv
Next

WScript.Echo vbCrLf & "Environment Process:"    & vbCrLf & _ 
       "..............................................."

For Each IEnv In WshShell.Environment("Process")
    WScript.Echo IEnv
Next
Daniel
  • 12,982
  • 3
  • 36
  • 60
STTR
  • 139
  • 2
  • 2
-1

This works for me:

Dim objNetwork
Set objNetwork = CreateObject("WScript.Network")
MsgBox objNetwork.UserName

or from the shell:

Set wshShell = WScript.CreateObject( "WScript.Shell" )
strUserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )

or from environment variable (it should work, but when i tested it was wrong!):

Set WshShell = CreateObject("WScript.Shell")
Set WshEnv = WshShell.Environment
MsgBox "USERNAME=" & WshEnv.Item("USERNAME")
Rafiki
  • 604
  • 6
  • 19
  • 2
    The [object returned by `CreateObject("WScript.Network"`](https://msdn.microsoft.com/en-us/library/s6wt333f(v=vs.84).aspx) does not provide generic environment-variable access, it just happens to expose 3 values as properties that are also reflected in environment variables: `ComputerName`, `UserDomain`, and `UserName`. The reason that your last example doesn't work is that `WshShell.Environment` is the same as `WshShell.Environment("System")`, and `%USERNAME%` isn't defined in the _system_ scope. – mklement0 Dec 20 '16 at 18:09