VBScript Environment Variables

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

Environment Variable Code
Environment Variable Code | Source

Variable Creation

Variable
Variable | Source

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.

Option explicit

'Declare Variables

Dim WshShl, Shell, UserVar

'Set objects

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

Set Shell = WshShl.Environment("User")

'Read variable

UserVar = Shell("Test1")

'Output value to msgbox

WScript.Echo "Your name is " & UserVar & "!"

'Cleanup Objects

Set WshShl = Nothing

Set Shell = Nothing

'Exit Script

WScript.Quit()

Save the file as “C:\Temp\readUsrVar.vbs”.

VBScript Messagebox

Messagebox
Messagebox | Source

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”

VBScript Messagebox

os variable
os variable | Source

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

  • Ping Script
    2

    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...

  • VBScript
    0

    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...

  • How to Remove a Mildew Smell From a Window Air Conditioner
    6

    Learn how to get rid of the musty, mildew-y smell coming from your window AC unit and stop the stink!


Comments 5 comments

rorshak sobchak 5 years ago

Great write up! Very detailed. Keep up the great work.


who10 profile image

who10 5 years ago from Midwest Author

Thanks!


Micael 4 years ago

Thank you


Johnd537 2 years ago

Thank you for your blog article. Great. cbckdgbebddg


who10 profile image

who10 9 months ago from Midwest Author

Thanks!

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working