VBScript Environment Variables
Applications make use of environment variables either by writing to or reading from them at different times during the runtime of a program. System administrators, IT analysts and anyone else using VBScript, can take advantage of the WshShl.Environment object to read these variables. They can be used as variables to pass on to other functions, or just gain status on the operation of a program.
Often times, applications will write license server information to these variables. Other times, the variables may be used to point an application to a test environment. Reading these variable is a great way for a VBScript to aquire server information or even switch between test and production environments.
VBScript Environment Variable Code
Create Environment Variable
Create a user environment variable by right-clicking on “My Computer”-->”properties”-->”Advanced” tab-->”Environment Variables and under “User variables for…”, select “New”. In the “New User Variable” window, within the “Variable name:” box, type “test1”. Within the “Variable value:” box, type “Bob”. Select “OK”. Select “OK” again to get out of the “Environment Variables” window, and then select “OK”.
Create VBScript to Read an Environment Variable
Go to “Start”-->”All Programs” -->”Accessories”-->”Notepad”. After Notepad opens up, save the file as “C:\Temp\readUsrVar.vbs”. Adding .vbs as a file extension creates a VBScript executable.
Copy the following code to the empty readUsrVar.vbs file. Select in front of the “O” in Option explicit, and drag to the right of the “)” in WScript.Quit(). This will highlight the code. Do an “Edit”-->”Copy” or “Ctrl” + “C” to copy the code. Do an “Edit”-->”Paste” or “Ctrl” + “V” to paste the code into the readUsrVar.vbs file.
Dim WshShl, Shell, UserVar
Set WshShl = WScript.CreateObject("WScript.Shell")
Set Shell = WshShl.Environment("User")
UserVar = Shell("Test1")
'Output value to msgbox
WScript.Echo "Your name is " & UserVar & "!"
Set WshShl = Nothing
Set Shell = Nothing
Save the file as “C:\Temp\readUsrVar.vbs”.
Run Environment Variable VBScript
Browse to “C:\Temp\readUsrVar.vbs” using “My Computer” and double-click on the readUsrVar.vbs file. This will run the VBscript that was just created. A message box will appear stating “Your name is Bob!” Select “OK”
Change to System Variable
If the requirement is to read a “System” environment variable instead of a “User” environment variable,
replace this line in the VBScript, “Set Shell = WshShl.Environment("User")”, with this line, “Set Shell = WshShl.Environment("System")”.
Replace this line, “UserVar = Shell("test1")”, with this line, “UserVar = Shell(“os”)”.
Note that the “os” system variable is being used as an example in the line above, .
Replace this line, “WScript.Echo "Your name is " & UserVar & "!"”, with this line, “WScript.Echo UserVar”
In order for the VBScript to make sense, the UserVar variable should probably be renamed to SysVar, but the script will function just fine without this change.
More by this Author
At times, when programming with VBScript, a programmer will want to know if a list of computers are connected to a network and powered on. Often times, prior to a major change, or when trouble shooting user issues, an...
Don’t overlook the power of this scripting language! Administrators, developers, analysts and others have all discovered the power of Microsoft VBScript. VBScript, sometimes called “VBScripting” is a...
Learn how to get rid of the musty, mildew-y smell coming from your window AC unit and stop the stink!