How to Use Environment Variable VBS Script

Environment variables are reserved keywords with a percent sign prefix and suffix. These keywords are designated by the Windows operating system. For instance, the environment variable "%username%" is a variable that displays the current username you used to log in to the computer. These variables are beneficial for programming VBS Script files. The environment variable allows you to make your scripts dynamic, so you can run them on any of your users' machines.

Instructions

    1

    Click the Windows "Start" button and then type "notepad" into the search text box. Press "Enter" to open the Notepad text editor, which is the main tool for creating VBS scripts.

    2

    Type "echo %variable%" into the text editor. Replace "variable" with the environment variable you want to use. For instance, if you want to display the username, replace "variable" with "username."

    3

    Get the computer name. Another common environment variable is the "ComputerName" variable. Use the following code to retrieve the computer name and display it to the user:

    Set shell = CreateObject( "WScript.Shell" )

    comp=shell.ExpandEnvironmentStrings("%ComputerName%")

    4

    Click the "File" menu item and select "Save As." Type "variables.vbs" into the "File name" text box. Select "All Files" in the "File type" drop-down box. Click "Save."

Blog Archive