Skip to main content

How to Run VBScript from a Batch Script

While batch scripting is excellent for file operations and command-line control, it has significant limitations, especially when it comes to GUI interaction or accessing certain Windows APIs. VBScript (Visual Basic Script) is a more powerful, built-in scripting language that can overcome many of these limitations. By calling a VBScript from your batch file, you can "supercharge" your scripts to perform tasks that are impossible in pure batch.

This guide will teach you how to use the cscript.exe command to execute VBScript files from your batch script. You will learn the difference between cscript and wscript, how to pass arguments to your VBScript, and see a practical example of how to display a graphical pop-up message box.

Why Use VBScript with a Batch File?

VBScript can do many things that batch can't. By calling a .vbs file, your batch script gains the ability to:

  • Create GUI elements: Display message boxes, input boxes, and file open/save dialogs.
  • Automate COM Objects: Interact with applications like Excel, Outlook, or Internet Explorer.
  • Access WMI: Perform advanced system queries in a more structured way than WMIC.
  • Perform complex string or math operations that are clumsy in pure batch.

Your batch script acts as the orchestrator, and the VBScript acts as a specialized tool.

The Core Command: cscript.exe

cscript.exe is the Command-line Script Host interpreter for the Windows Script Host. It is designed to run VBScript (and JScript) files and return their output to the command console, making it the perfect tool for batch scripting.

Syntax: cscript //nologo "MyScript.vbs" [Argument1] [Argument2]

  • cscript: The executable.
  • //nologo: A highly recommended switch that suppresses the Microsoft copyright banner from appearing in your output.
  • "MyScript.vbs": The path to the VBScript file to execute.
  • [Arguments]: Any arguments you want to pass to the script.

Basic Example: A Simple "Hello, World!"

Let's create a simple VBScript and call it from a batch file.

hello.vbs
' This is a simple VBScript
WScript.Echo "Hello from VBScript!"
note

WScript.Echo: This is the VBScript equivalent of batch's ECHO command. It prints text to the standard output stream.

And the following is the Batch Script:

run_hello.bat
@ECHO OFF
ECHO --- Calling a VBScript from a Batch File ---
ECHO.

cscript //nologo "hello.vbs"

ECHO.
ECHO --- Returned to the Batch File ---

Output:

--- Calling a VBScript from a Batch File ---

Hello from VBScript!

--- Returned to the Batch File ---

How to Pass Arguments from Batch to VBScript

You can easily pass data from your batch script to your VBScript. The arguments are available inside the VBScript through the WScript.Arguments collection.

The Batch Script:

pass_args.bat
@ECHO OFF
SET "UserName=John Doe"
SET "City=New York"
cscript //nologo "greet_user.vbs" "%UserName%" "%City%"

The VBScript:

greet_user.vbs
Set objArgs = WScript.Arguments
strName = objArgs(0)
strCity = objArgs(1)

WScript.Echo "Welcome, " & strName & " from " & strCity & "!"
note
  • WScript.Arguments: A collection of all arguments passed to the script.
  • objArgs(0): Accesses the first argument (it is zero-indexed).

Output:

Welcome, John Doe from New York!

Common Pitfalls and How to Solve Them

Problem: cscript vs. wscript

Windows has a second script interpreter called wscript.exe (Windows Script Host).

  • cscript.exe: For command-line operations. Output goes to the console (stdout). This is what you want for batch scripts.
  • wscript.exe: For Windows GUI operations. Output from WScript.Echo appears in a graphical message box. This will halt your script, waiting for a user to click "OK."

Solution: Always use cscript.exe when calling VBScript from a batch file to ensure the I/O is handled correctly in the console.

Problem: The Temporary VBScript File

For simple tasks, it can be clumsy to have two separate files (.bat and .vbs).

Solution: You can have your batch script dynamically create the VBScript file in the temporary directory, run it, and then delete it. This keeps your entire logic self-contained in a single .bat file.

Practical Example: Displaying a GUI Message Box

This is a classic use case. This batch script uses the "dynamic VBScript" method to show a graphical pop-up message to the user, a task that is impossible in pure batch.

@ECHO OFF
SETLOCAL
SET "MESSAGE_TITLE=Task Complete"
SET "MESSAGE_TEXT=The backup operation has finished successfully."
SET "TEMP_VBS=%TEMP%\~messagebox.vbs"

ECHO --- GUI Message Box from Batch ---
ECHO.
ECHO This script will now display a graphical pop-up.

REM --- Create the VBScript dynamically ---
(
ECHO Set WshShell = WScript.CreateObject("WScript.Shell")
ECHO WshShell.Popup "%MESSAGE_TEXT%", 0, "%MESSAGE_TITLE%", 0
) > "%TEMP_VBS%"

REM --- Execute the VBScript using WSCRIPT for a GUI effect ---
REM Here, we use wscript because we WANT a graphical pop-up.
wscript //nologo "%TEMP_VBS%"

REM --- Clean up the temporary file ---
IF EXIST "%TEMP_VBS%" DEL "%TEMP_VBS%"

ECHO.
ECHO --- Pop-up has been closed. Script is done. ---
ENDLOCAL

Conclusion

Calling VBScript is a powerful technique that allows you to break free from the limitations of the standard cmd.exe environment. It lets you add GUI interactions and perform complex tasks that would otherwise be impossible.

Key takeaways:

  • Use cscript.exe to execute VBScript files and interact with them in the command line.
  • Use //nologo to keep your output clean.
  • You can pass arguments from your batch script to the VBScript, which can be accessed via the WScript.Arguments collection.
  • For self-contained scripts, you can have your batch file dynamically create and delete the .vbs file.
  • For GUI tasks like pop-up messages, use wscript.exe instead of cscript.exe.