How to Create a Simple Menu System in a Batch Script
A menu is one of the best ways to make a batch script interactive and user-friendly. Instead of forcing a user to remember command-line arguments, you can present them with a numbered or lettered list of options. This turns a simple script into a guided tool, making it more accessible and easier to use.
This guide will teach you how to create a robust menu system using a combination of ECHO commands to display the options, the CHOICE command to capture a single keypress without requiring Enter, and GOTO commands to direct the script's flow based on the user's selection.
The Core Components of a Menu System
A good menu script is built like a loop and has three main parts:
- Display the Menu: Clear the screen and use a series of
ECHOcommands to show the user their options in a clean, readable format. This part of the code is usually marked with a label (e.g.,:Menu). - Get User Input: Wait for the user to select an option. The
CHOICEcommand is perfect for this, as it captures a single keypress instantly. - Process the Choice: Use a series of
IF ERRORLEVELorIF "%ERRORLEVEL%"statements to determine which key was pressed and then useGOTOto jump to the section of the script that handles that specific action. After the action is complete, it usually jumps back to the:Menulabel.
The Core Command for Input: CHOICE
While SET /P can get input, it requires the user to press Enter. The CHOICE command is far superior for menus because it reacts to a single keypress. It communicates the user's selection back to the script by setting the %ERRORLEVEL% variable to the 1-based index of the key pressed.
Syntax for a menu:
CHOICE /C 123 /N /M "Your choice: "
/C 123: Specifies the valid Choices are the keys1,2, and3./N: No prompt. This hides the default[1,2,3]?text, allowing us to create a clean, custom prompt./M "...": The Message to display to the user.
The Script: A Basic Numbered Menu
This script demonstrates the fundamental loop and logic of a menu system.
@ECHO OFF
:MainMenu
CLS
ECHO ===========================
ECHO MY SCRIPT MENU
ECHO ===========================
ECHO.
ECHO 1. Display Hello
ECHO 2. Display Goodbye
ECHO 3. Exit
ECHO.
CHOICE /C 123 /N /M "Please enter your choice [1,2,3]: "
REM --- Process the choice. Remember to check ERRORLEVEL from HIGHEST to LOWEST ---
IF ERRORLEVEL 3 GOTO :ExitScript
IF ERRORLEVEL 2 GOTO :Option2
IF ERRORLEVEL 1 GOTO :Option1
:Option1
ECHO.
ECHO Hello, World!
PAUSE
GOTO :MainMenu
:Option2
ECHO.
ECHO Goodbye!
PAUSE
GOTO :MainMenu
:ExitScript
ECHO.
ECHO Exiting the script.
EXIT /B
How the Menu Script works:
:MainMenu: A label that marks the start of our menu loop.CLS: The Clear Screen command is essential. It wipes the screen of any previous output, ensuring the menu is always displayed cleanly at the top.ECHO ...: A series ofECHOcommands prints the menu options.CHOICE ...: This command pauses the script and waits for the user to press1,2, or3.IF ERRORLEVEL ...: This is the decision-making block. BecauseIF ERRORLEVEL Nmeans "if the error level is N or greater," we must check for the highest number (3) first.GOTO :OptionX: The script jumps to the label corresponding to the user's choice.PAUSE: At the end of an option's logic,PAUSEis used to hold the output on the screen until the user is ready to return to the menu.GOTO :MainMenu: After the pause, this command sends the script back to the beginning of the loop to display the menu again.
Key CHOICE Parameters for Menus
/C <Choices>: (Required) The list of valid keys. You can use letters too, e.g.,/C ABCQ./N: Hides the default prompt text ([A,B,C,Q]?). Essential for a clean menu interface./M <Message>: The text to display as the prompt./T <Seconds> /D <Choice>: Adds a Timeout. If the user doesn't press a key in<Seconds>, the Default choice<Choice>is automatically selected.
Common Pitfalls and How to Solve Them
-
Incorrect
ERRORLEVELOrder: This is the #1 mistake. CheckingIF ERRORLEVEL 1first will cause the script to always select Option 1, because all choices (1, 2, and 3) are "1 or greater." Solution: Always check from highest to lowest. -
Screen Clutter: Forgetting to put
CLSat the top of the menu loop will cause the menu to be reprinted below the output of the last command, creating a messy and confusing display. Solution: Always start your menu display block withCLS. -
Forgetting to Loop: Forgetting the
GOTO :MainMenuat the end of an option's block will cause the script to "fall through" and execute the code for the next option. Solution: Ensure every option block ends with aGOTOthat either returns to the menu or exits the script.
Practical Example: A System Utilities Toolbox
This script provides a user with a handy menu of common diagnostic tools.
@ECHO OFF
:Menu
CLS
ECHO ===============================
ECHO SYSTEM UTILITIES
ECHO ===============================
ECHO 1. Show Network Configuration
ECHO 2. Check for Active Connections
ECHO 3. Ping Google's DNS
ECHO 4. Exit
ECHO.
CHOICE /C 1234 /N /M "Select a utility to run [1-4]: "
IF ERRORLEVEL 4 GOTO :EOF
IF ERRORLEVEL 3 GOTO :RunPing
IF ERRORLEVEL 2 GOTO :RunNetstat
IF ERRORLEVEL 1 GOTO :RunIpconfig
:RunIpconfig
CLS
ECHO --- Network Configuration ---
ipconfig /all
PAUSE
GOTO :Menu
:RunNetstat
CLS
ECHO --- Active Connections ---
netstat -an
PAUSE
GOTO :Menu
:RunPing
CLS
ECHO --- Pinging 8.8.8.8 ---
ping 8.8.8.8
PAUSE
GOTO :Menu
Conclusion
A menu system transforms a simple script into a user-friendly application. The CHOICE command is the ideal tool for this, providing instant, single-keypress input.
For a robust and effective menu:
- Structure your script as a loop, starting with a label (e.g.,
:Menu). - Always begin your display block with
CLSfor a clean screen. - Use
CHOICE /C ...to get the user's input. - Process the result by checking
%ERRORLEVEL%from highest to lowest. - Use
GOTOto direct the script flow, and always loop back to the menu label after an action is complete.