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.)
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.)
Here's an example (taken from here):
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:
This is the equivalent of (note the absence of
%
around the variable name):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.:On rare occasions you may have to access environment-variable definitions from a specific scope (other than the current process's).
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.From here ...
Also, much more detail on TechNet.
This works for me:
or from the shell:
or from environment variable (it should work, but when i tested it was wrong!):