How to Pass Parameters to VBScript

Friday, October 28, 2011

If you have a VBScript program which would be better utilized if you can pass one or more parameters at run time, you can easily do so by accepting them as arguments.

Here is an example of how the program would be run from the command prompt (Separate the parameters with spaces).

filename.vbs parameter1 parameter2

Use the WScript.Arguments to retrieve the parameters that were passed.

Dim Arg, var1, var2
Set Arg = WScript.Arguments

'Parameter1, begin with index0
var1 = Arg(0)

var2 = Arg(1)

msgbox "First parameter passed was " _
       & var1 & " and second parameter passed was " & var2

'Clear the objects at the end of your script.
set Arg = Nothing

You can use the approach for passing parameters in a command shell, or even a scheduled task. Passing parameters to a VBScript file allows you to reuse your VBScript program.

Did you find the page informational and useful? Share it using one of your favorite social sites.

Recommended Books & Training Resources

Advanced VBScript for Microsoft Windows Administrators Microsoft WSH and VBScript Programming for the Absolute Beginner