VBScript – How To Pass variables to a VBS Script

I was recently was working on how to pass command line variables to a VBS file.  This article shows how to pass user-defined parameters to a Windows Scripting Host file from a command line.

Example of what would be typed at the command line

wscript filename.vbs parameter1 parameter2

In the command line this needs to start with wscript (VBScript interpreter) or cscript (JScript compiler).  Filename.vbs(Name of the file.  VBS and JS can be the extensions) and separated by spaces parameter1, parameter2 etc.  Make sure the parameters have spaces between them.

Retrieve parameters from File

Using the Args statement helps retrieve the space delimited array of parameters that were executed in the command line.

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

'First parameter

var1 = ArgObj(0)

'Second parameter

var2 = ArgObj(1)
msgbox "Variable 1=" & var1 & " Variable 2=" & var2

'Clear object out of memory

set ArgObj = Nothing

Batch file to execute file

Either a .bat or .cmd file can be used to execute the file.  Another technique would be to use Windows Task Scheduler to schedule this file to be executed on a timed basis.

mybatchfile.cmd

wscript filename.vbs Parameter1 Parameter2
SOURCE

LINK

LANGUAGE
ENGLISH