How to Use the CHOICE Command for User Input in Batch Script
Creating interactive scripts that present users with a menu of options is a great way to build powerful and user-friendly tools. While SET /P can capture any string a user types, it's often better to restrict their input to a specific set of valid keys (like Y/N). For this, the CHOICE command is the perfect tool.
This guide will teach you how to use the built-in CHOICE command to create simple and effective user menus. You will learn how to define the allowed keys, how to read the user's selection using %ERRORLEVEL%, and how to add features like a timeout and a default option.
The Core Command: CHOICE
The CHOICE command is designed for one specific task: to pause the script, prompt the user to press one of a predefined set of keys, and report which key was pressed. It is far more controlled than the free-form SET /P command.
The basic syntax is:
CHOICE /C <Choices> /M "<Message>"
/C: Defines the set of valid Choices./M: Defines the prompt Message to show the user.
Basic Example: A Simple Yes/No Prompt
This script asks the user a simple question and waits for them to press either the Y or N key.
@ECHO OFF
ECHO --- Simple Yes/No Prompt ---
CHOICE /C YN /M "Do you want to continue?"
REM Logic to handle the choice will go here.
ECHO.
ECHO The script continues...
The script will display the message and a prompt showing the valid keys, then wait for a keypress.
--- Simple Yes/No Prompt ---
Do you want to continue? [Y,N]?
Once the user presses Y or N (but not any other key), the command ends, and the script proceeds.
How to Read the User's Selection (%ERRORLEVEL%)
This is the most critical concept for using CHOICE. The command does not set a variable; instead, it communicates the user's selection by setting the %ERRORLEVEL% system variable.
The %ERRORLEVEL% is set to the 1-based index of the key that was pressed in the choice list.
For CHOICE /C YN:
- If the user presses
Y,%ERRORLEVEL%will be 1. - If the user presses
N,%ERRORLEVEL%will be 2. - If the user presses
Ctrl+Cto abort,%ERRORLEVEL%will be 0.
An example of script:
@ECHO OFF
CHOICE /C YN /M "Do you agree?"
ECHO.
IF %ERRORLEVEL% EQU 2 (
ECHO You chose 'N'. Exiting.
GOTO :EOF
)
IF %ERRORLEVEL% EQU 1 (
ECHO You chose 'Y'. Proceeding...
REM (More commands would go here)
)
Key CHOICE Parameters Explained
/C <Choices>: (Required) The list of valid keys. Example:/C 123or/C YNC./M "<Message>": The prompt Message to display./N: No prompt. Hides the default[Y,N]?text, giving you full control over the prompt's appearance./T <Timeout>: Sets a Timeout in seconds. If the user doesn't press a key in time, a default choice is made./D <DefaultChoice>: Specifies the Default choice to be used with/T. This must be a character from your/Clist.
Common Pitfalls and How to Solve Them
Problem: The ERRORLEVEL Check Order is Important
If you use the classic IF ERRORLEVEL N syntax, you must check the values in descending order. This is because IF ERRORLEVEL N means "if the error level is N or greater".
Example of script with error:
REM This is WRONG. The first check will always be true.
IF ERRORLEVEL 1 ECHO You chose Yes.
IF ERRORLEVEL 2 ECHO You chose No.
Solution: Use EQU or Check in Descending Order
- Use
EQU(Recommended): The modernIF %ERRORLEVEL% EQU Nsyntax is exact and unambiguous. The order doesn't matter. This is the best practice.IF %ERRORLEVEL% EQU 2 GOTO :ChoiceNo
IF %ERRORLEVEL% EQU 1 GOTO :ChoiceYes - Descending Order: If you must use the classic syntax, check from highest to lowest.
IF ERRORLEVEL 2 GOTO :ChoiceNo
IF ERRORLEVEL 1 GOTO :ChoiceYes
Problem: The Prompt is Case-Sensitive
By default, /C YN only accepts uppercase Y and N.
Solution: Include Both Cases
The easiest way to handle this is to include both cases in your choice list and structure your logic accordingly.
CHOICE /C yYnN /M "Proceed? (y/n)"
REM y=1, Y=2, n=3, N=4
IF %ERRORLEVEL% GEQ 3 GOTO :No
IF %ERRORLEVEL% GEQ 1 GOTO :Yes
Here, we use GEQ (Greater than or Equal to) to group the lowercase and uppercase results.
Problem: Adding a Timeout and Default Choice
For unattended scripts, you can combine /T and /D to proceed automatically if there is no user interaction.
CHOICE /C YN /T 10 /D Y /M "Start backup now? Defaulting to YES in 10 seconds."
This will wait 10 seconds, and if no key is pressed, it will automatically select Y and continue.
Practical Example: A Multi-Option Menu
This script presents the user with a menu of actions to perform. CHOICE is perfect for this, as it prevents the user from typing an invalid option.
@ECHO OFF
SETLOCAL
:Menu
CLS
ECHO --- System Maintenance Menu ---
ECHO.
ECHO 1. Check Disk Space
ECHO 2. Ping Gateway
ECHO 3. View IP Configuration
ECHO Q. Quit
ECHO.
CHOICE /C 123Q /N /M "Please enter your choice: "
REM Use modern, exact checks for ERRORLEVEL
IF %ERRORLEVEL% EQU 4 GOTO :Quit
IF %ERRORLEVEL% EQU 3 GOTO :ViewIP
IF %ERRORLEVEL% EQU 2 GOTO :PingGateway
IF %ERRORLEVEL% EQU 1 GOTO :CheckDisk
:CheckDisk
ECHO --- Checking Disk Space ---
FSUTIL volume diskfree C:
PAUSE
GOTO :Menu
:PingGateway
ECHO --- Pinging Gateway ---
PING -n 4 192.168.1.1
PAUSE
GOTO :Menu
:ViewIP
ECHO --- Viewing IP Configuration ---
IPCONFIG /ALL
PAUSE
GOTO :Menu
:Quit
ECHO Exiting.
ENDLOCAL
Conclusion
The CHOICE command is the ideal tool for creating simple, controlled user prompts in batch scripts. It is far more robust than SET /P for menu-driven applications because it restricts the user's input to only the valid options you define.
Key takeaways:
- Use
/Cto define the set of allowed keys. - Read the user's selection from the
%ERRORLEVEL%variable, which corresponds to the 1-based index of the choice. - Use the modern
IF %ERRORLEVEL% EQU Nsyntax for clear and unambiguous checks. - Use
/Tand/Dto create timed prompts that can default to a specific action in unattended scripts.