Skip to main content

How to Display a Pop-up Message Box in Batch Script (via VBScript)

The standard batch script environment is text-only. While ECHO can print messages to the console, it cannot create graphical, user-friendly pop-up windows. For tasks that require a more visible notification or a simple GUI confirmation from the user, you need to step outside of cmd.exe's native capabilities.

The classic, most compatible method for achieving this is to have your batch script dynamically generate and execute a small VBScript file. This powerful technique allows you to leverage the Windows Script Host to create message boxes with custom buttons, icons, and titles, and even capture the user's response.

The Challenge: No Native GUI Commands

The command prompt is a text-based interface. It has no built-in command like MSGBOX "My Message". To create a graphical element like a pop-up window, we must call on a different Windows subsystem that can, like the Windows Script Host (WScript.exe).

The Core Method: Generating a Temporary VBScript

The standard workaround is a three-step, self-contained process:

  1. Write: The batch script uses ECHO commands to write a few lines of VBScript code into a temporary file (e.g., %TEMP%\popup.vbs).
  2. Execute: The batch script then uses WScript.exe to run this temporary VBScript file. WScript.exe is the Windows host for GUI scripts, so it runs without creating a new console window.
  3. Cleanup: After the user closes the pop-up, the batch script deletes the temporary .vbs file.

Basic Example: A Simple "Hello World" Pop-up

This script creates and executes the simplest possible message box.

@ECHO OFF
SET "MESSAGE=Hello World! This is a pop-up from a batch script."
SET "TITLE=My Script"
SET "VBS_FILE=%TEMP%\popup.vbs"

ECHO --- Creating a pop-up message box ---

REM --- Step 1: Write the VBScript file ---
ECHO MsgBox "%MESSAGE%", 0, "%TITLE%" > "%VBS_FILE%"

REM --- Step 2: Execute the VBScript file ---
REM The /B switch prevents the script from creating a new command window
START /B wscript.exe "%VBS_FILE%"

REM --- Step 3: Cleanup ---
REM This step is often omitted to keep the example simple, but is good practice.
REM DEL "%VBS_FILE%"

ECHO The script can continue while the pop-up is open...

When you run this, a standard Windows message box will appear on the screen.

Customizing the Message Box (Buttons, Icons, and Title)

The VBScript MsgBox function is very flexible. Its syntax is MsgBox "Message", [Buttons+Icon], "Title". The middle number is a code that defines the buttons and icon.

Common Button Codes:

  • 0: OK button
  • 1: OK and Cancel buttons
  • 3: Abort, Retry, and Ignore buttons
  • 4: Yes and No buttons

Common Icon Codes:

  • 16: Critical (Stop sign) icon
  • 32: Question mark icon
  • 48: Warning (exclamation point) icon
  • 64: Information (letter 'i') icon
note

You create the final code by adding a button code and an icon code. For a Yes/No prompt with a question mark icon, the code is 4 + 32 = 36.

Example of script with customized pop-up:

@ECHO OFF
SET "MESSAGE=Do you want to proceed with the operation?"
SET "TITLE=Confirmation Required"
SET "BUTTONS_ICONS=36"
SET "VBS_FILE=%TEMP%\popup.vbs"

ECHO MsgBox "%MESSAGE%", %BUTTONS_ICONS%, "%TITLE%" > "%VBS_FILE%"
START /B wscript.exe "%VBS_FILE%"

Getting a Response from the User (Capturing Clicks)

This is the most powerful feature. When the user clicks a button, WScript.exe exits with a specific exit code (ERRORLEVEL) that your batch script can check.

Return Codes (ERRORLEVEL):

  • 1: OK
  • 2: Cancel
  • 6: Yes
  • 7: No

To capture this, you must run wscript.exe in a way that your script waits for it to finish.

Example of script capturing a yes/no response:

@ECHO OFF
SET "VBS_FILE=%TEMP%\popup.vbs"
SET "MESSAGE=Do you agree to the terms?"
SET "TITLE=Agreement"

REM Create a VBScript that returns the user's choice
ECHO WScript.Quit(MsgBox("%MESSAGE%", 4 + 32, "%TITLE%")) > "%VBS_FILE%"

REM Run the script and WAIT for it to finish.
cscript //nologo "%VBS_FILE%"

REM --- Check the ERRORLEVEL ---
IF %ERRORLEVEL% EQU 6 (
ECHO You clicked YES.
)
IF %ERRORLEVEL% EQU 7 (
ECHO You clicked NO.
)

DEL "%VBS_FILE%"
note

We use cscript here because it's simpler to make the batch file wait for it to finish.

Common Pitfalls and How to Solve Them

  • Quotes in the Message: If your message string contains double quotes, it will break the VBScript syntax.
    • Solution: You must "escape" the quotes for VBScript by doubling them up. SET "MESSAGE=He said, ""Hello!"" to me."
  • Script Continues Without Waiting: Using START /B wscript.exe is great for simple notifications, as your batch script can continue its work. If you need to wait for the user's answer, you must run cscript.exe or a blocking wscript call.

Practical Example: A "Confirm Deletion" Script

This script uses a customized, blocking pop-up to ask the user for confirmation before deleting a file.

@ECHO OFF
SETLOCAL
SET "FILE_TO_DELETE=important_data.txt"
SET "VBS_FILE=%TEMP%\confirm.vbs"

ECHO --- Deletion Script ---
ECHO.

REM --- Create the VBScript with a Yes/No prompt and a warning icon ---
SET "MESSAGE=Are you sure you want to permanently delete %FILE_TO_DELETE%?"
SET "TITLE=Confirm Deletion"
SET "BUTTONS=4"
SET "ICON=48"
SET /A "VB_CODE=%BUTTONS% + %ICON%"

ECHO WScript.Quit(MsgBox("%MESSAGE%", %VB_CODE%, "%TITLE%")) > "%VBS_FILE%"

REM --- Run the script and wait for the user's choice ---
cscript //nologo "%VBS_FILE%"
SET "UserChoice=%ERRORLEVEL%"
DEL "%VBS_FILE%"

REM --- Act on the choice ---
IF "%UserChoice%"=="6" (
ECHO You clicked YES. Deleting the file...
REM DEL "%FILE_TO_DELETE%"
) ELSE (
ECHO You clicked NO. Operation cancelled.
)

ENDLOCAL

Conclusion

Generating a temporary VBScript is the classic and most compatible method for creating graphical message boxes from a batch file. It provides a level of user interaction that ECHO and PAUSE cannot match.

Key takeaways:

  • The core method is to ECHO VBScript code to a .vbs file, execute it with wscript.exe or cscript.exe, and then delete the file.
  • You can customize the title, message, buttons, and icon by changing the parameters of the MsgBox function.
  • To capture the user's response, use cscript.exe and check the %ERRORLEVEL% after it returns.