ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming

VBScript Environment Variables

Updated on August 6, 2016

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


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

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


    0 of 8192 characters used
    Post Comment

    • who10 profile image

      who10 22 months ago from Midwest


    • profile image

      Johnd537 3 years ago

      Thank you for your blog article. Great. cbckdgbebddg

    • profile image

      Micael 5 years ago

      Thank you

    • who10 profile image

      who10 6 years ago from Midwest


    • profile image

      rorshak sobchak 6 years ago

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