eHow launches Android app: Get the best of eHow on the go.

How To

How to Read a User/System Environment Variable Using VBScript

Member
By who10
User-Submitted Article
(1 Ratings)
Information Technology
Information Technology
John C. Hooten

At times, when programming with vbscript, a programmer will want to read an existing environment variable.

This can be accomplished by accessing the Windows Script
Host via VBScript and making use of the shell object

The program example below will walk you through the steps necessary to accomplish this task and give you a real world example to learn from.

Difficulty: Moderately Easy
Instructions

Things You'll Need:

  • Computer
  • Windows(98 or newer)
  1. Step 1

    1. Create a User Environment Variable

    Create a user environment variable by right-clicking on “My Computer”, selecting Properties – Advanced tab - Environment Variables and under User variables select New. This will open a New User Variable window. For Variable name:, type test. For Variable value:, type your name and select OK.

  2. Step 2

    2. Open Notepad

    Go to Start - All Programs - Accessories - Notepad
    Save your file as C:\Temp\ReadEnvVar.vbs

  3. Step 3

    3. Copy Code

    Copy and Paste the following code into Notepad.

    '**************************************************************
    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("Test")

    'Output value to msgbox
    WScript.Echo "Your name is " & UserVar & "!"

    'Cleanup Objects
    Set WshShl = Nothing
    Set Shell = Nothing

    'Exit Script
    WScript.Quit()
    '**************************************************************
    Save your file and exit Notepad

  4. Step 4
    Your Name
     
    Your Name

    4.Run Script

    Browse to C:\Temp\ReadEnvVar.vbs using My Computer or Explore, depending on what you're use to.
    Double-click on ReadEnvVar.vbs. You should get a message box as shown stating your name.

    Select "OK"

    If you prefer to read a System environment variable, replace this line in your script
    Set Shell = WshShl.Environment("User")
    With this line
    Set Shell = WshShl.Environment("System")

    Replace this line
    UserVar = Shell("Test")
    With this line.
    UserVar = Shell("Insert system variable you want to read here")

    Replace this line
    WScript.Echo "Your name is " & UserVar & "!"
    With this line
    WScript.Echo UserVar

    So your vbscript makes sense, you may also want to replace the UserVar variable with SysVar, but your script should function just fine without this change.

  5. Step 5

    Congratulations! You've written a VBScript capable of reading a user or system environment variable.

Tips & Warnings
  • When working with vbscript(.vbs) files you'll want to right-click on them and choose Edit to modify them, otherwise you will run the script.
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy.   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License.

eHow Computers
eHow_eHow Technology and Electronics