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.

Please help us spread the word by socializing it today!

email contact us

Did you find something wrong with the information on this page? Please take a moment to report it to us so that we can continue to improve the quality of the information on this site. Click here to report an issue with this page.

Recommended Books & Training Resources

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