choiceCHOICE Command Guide - User Selection in Windows Batch Scripts
Learn how to use the CHOICE command to prompt users to select from options in Windows batch scripts. Includes syntax, examples, and ERRORLEVEL handling.
The CHOICE command is a Windows Command Prompt utility that displays a prompt and waits for the user to select one option from a set of choices (e.g., Y or N). It sets ERRORLEVEL to indicate which choice was selected, making it ideal for conditional logic in batch scripts. Use /C to specify the choice characters, /M for a custom message, and /T for a timeout with default selection. Script authors and system administrators use CHOICE for interactive menus, confirmation prompts, and automated scripts that need user input.
Whether you're building a batch menu that lets users pick options (1, 2, 3), asking for Y/N confirmation before a destructive operation, or creating a timeout that defaults to "No" if the user doesn't respond, CHOICE provides a simple, built-in way to get user input without external tools. It has been part of Windows since Windows 2000 and is available in all modern Windows versions.
This comprehensive guide covers CHOICE syntax, all parameters, practical examples for common scenarios, ERRORLEVEL handling, troubleshooting tips, and frequently asked questions. By the end, you'll confidently add interactive prompts to your batch scripts.
What Is the CHOICE Command?
The CHOICE command displays a prompt with a set of allowed characters (e.g., [Y,N]?) and waits for the user to press one of those keys. It then sets ERRORLEVEL to the 1-based index of the selected choice (1 for first, 2 for second, etc.) and exits. Batch scripts use IF ERRORLEVEL to branch based on the selection.
CHOICE runs in Command Prompt (CMD) and requires no special privileges. It is designed for batch scripts but can be run interactively. The command is not available in older Windows versions (e.g., Windows 95/98); use alternative methods like SET /P on those systems.
CHOICE Command Syntax
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
Parameters and Switches
| Parameter | Description |
|---|---|
/C choices | Specifies the list of choice characters (e.g., YN, 123, ABC). Default: YN |
/N | Hides the choice list (e.g., don't show [Y,N]?) |
/CS | Case-sensitive (default is case-insensitive) |
/T timeout | Timeout in seconds; use with /D for default |
/D choice | Default choice if timeout expires (use with /T) |
/M text | Message to display before the prompt |
If the user presses an invalid key, CHOICE beeps and waits again. Pressing Ctrl+C aborts and sets ERRORLEVEL to 0 (or 255 on some versions).
CHOICE and ERRORLEVEL
CHOICE sets ERRORLEVEL as follows:
- 1 – First choice selected
- 2 – Second choice selected
- 3 – Third choice selected
- ... and so on
- 0 or 255 – User pressed Ctrl+C or invalid/abort
In batch scripts, use:
IF ERRORLEVEL 2 goto second
IF ERRORLEVEL 1 goto first
Important: IF ERRORLEVEL N is true when ERRORLEVEL is greater than or equal to N. So check higher values first (e.g., ERRORLEVEL 2 before ERRORLEVEL 1).
Practical CHOICE Command Examples
Simple Y/N Prompt
To ask for Yes or No:
CHOICE /C YN /M "Do you want to continue?"
IF ERRORLEVEL 2 goto no
IF ERRORLEVEL 1 goto yes
Explanation: /C YN allows Y or N. /M shows the message. ERRORLEVEL 1 = Y (first choice), ERRORLEVEL 2 = N (second choice). Use for confirmation before critical operations.
Y/N/C for Yes, No, Cancel
To offer three options:
CHOICE /C YNC /M "Continue, Skip, or Cancel?"
IF ERRORLEVEL 3 goto cancel
IF ERRORLEVEL 2 goto skip
IF ERRORLEVEL 1 goto continue
Explanation: /C YNC defines three choices. Check ERRORLEVEL 3 first (Cancel), then 2 (Skip), then 1 (Continue). Order matters because of how IF ERRORLEVEL works.
Numeric Menu (1, 2, 3)
To create a simple menu:
CHOICE /C 123 /M "Select option: 1=Backup, 2=Restore, 3=Exit"
IF ERRORLEVEL 3 goto exit
IF ERRORLEVEL 2 goto restore
IF ERRORLEVEL 1 goto backup
Explanation: /C 123 uses digits. Map each ERRORLEVEL to a label. Use for multi-option batch menus.
Timeout with Default
To auto-select after 10 seconds if no input:
CHOICE /C YN /T 10 /D N /M "Proceed with install? (10 sec default: N)"
IF ERRORLEVEL 2 goto no
IF ERRORLEVEL 1 goto yes
Explanation: /T 10 sets 10-second timeout. /D N makes N the default if the user doesn't press a key. Use for unattended or low-interaction scripts.
Case-Sensitive Choices
To distinguish uppercase from lowercase:
CHOICE /C Ab /CS /M "Select A or b (case-sensitive)"
Explanation: /CS makes A and b different choices. Use when case matters (e.g., A=Admin, a=abort).
Hide Choice List
To show only the message, not [Y,N]?:
CHOICE /C YN /N /M "Press Y or N: "
Explanation: /N suppresses the choice list. Useful when you want a cleaner prompt or custom formatting.
Confirmation Before Delete
To confirm before deleting files:
CHOICE /C YN /M "Delete all files in temp? (Y/N)"
IF ERRORLEVEL 2 goto abort
del /Q C:\Temp\*.*
goto done
:abort
echo Cancelled.
:done
Explanation: Standard pattern for destructive operations. If user selects N (ERRORLEVEL 2), skip the delete and goto abort.
Batch Menu with Multiple Options
Full menu example:
@echo off
:menu
CHOICE /C 1234 /M "1=Backup 2=Restore 3=Logs 4=Exit"
IF ERRORLEVEL 4 goto exit
IF ERRORLEVEL 3 goto logs
IF ERRORLEVEL 2 goto restore
IF ERRORLEVEL 1 goto backup
goto menu
:backup
echo Running backup...
goto menu
:restore
echo Running restore...
goto menu
:logs
echo Showing logs...
goto menu
:exit
exit
Explanation: Loop menu with CHOICE. Each option branches to a label. User can keep selecting until option 4 (Exit).
Safe Default for Unattended
Use N as default when timeout expires for safety:
CHOICE /C YN /T 5 /D N /M "Apply changes? (5 sec, default N)"
IF ERRORLEVEL 2 goto no
REM Only reaches here if user explicitly chose Y
Explanation: If user doesn't respond in 5 seconds, N is selected. Prevents accidental "yes" in unattended scenarios.
Custom Message with Special Characters
To include a colon or other characters:
CHOICE /C YN /M "Warning: This will overwrite files. Continue?"
Explanation: /M accepts most text. Use for clear, descriptive prompts. Avoid characters that might conflict with batch syntax (e.g., ) in some contexts).
Common Use Cases for the CHOICE Command
-
Confirmation before destructive operations – Use CHOICE before DEL, FORMAT, or other risky commands. Default to N for safety.
-
Interactive batch menus – Build multi-option menus (1=Backup, 2=Restore, 3=Exit) with CHOICE and ERRORLEVEL branching.
-
Installation scripts – Ask "Install optional components? (Y/N)" with CHOICE. Use timeout and default for silent/unattended installs.
-
Maintenance scripts – "Run full scan? (Y/N)" or "Apply updates now? (Y/N)" with CHOICE for operator control.
-
Troubleshooting wizards – Step-through scripts that ask "Did this fix the issue? (Y/N)" and branch accordingly.
-
Backup/restore scripts – "Backup to local (1) or network (2)?" with CHOICE for destination selection.
-
Scheduled task confirmation – When a scheduled task runs, use CHOICE with timeout to allow operator override (e.g., "Skip this run? (Y/N) - 30 sec default N").
-
Deployment scripts – "Deploy to Production (1) or Staging (2)?" to prevent accidental production deployment.
-
Log and report options – "View logs (1), Email report (2), Exit (3)" for post-job actions.
-
Retry logic – "Retry? (Y/N)" after a failed operation. Loop on Y, exit on N.
-
Multi-step wizards – Chain multiple CHOICE prompts for complex workflows (e.g., select source, then destination, then confirm).
-
Training and demos – Use CHOICE to pause and let the user drive the flow in training scripts.
Tips and Best Practices
-
Check higher ERRORLEVELs first –
IF ERRORLEVEL Nis true when ERRORLEVEL >= N. Always check 3 before 2, 2 before 1. -
Use safe defaults with /T – When using timeout, set
/D Nor a non-destructive default. Avoid defaulting to "Yes" for dangerous operations. -
Test all branches – Run your script and press each choice to verify every branch works. Don't assume ERRORLEVEL mapping.
-
Handle Ctrl+C – User can press Ctrl+C to abort. ERRORLEVEL may be 0 or 255. Add
IF ERRORLEVEL 0 goto abort(or similar) if needed. -
Keep choices simple – Limit to a few options (e.g., YN or 1234). Too many choices confuse users and complicate branching.
-
Use clear /M messages – Write messages that explain what each choice does. "Continue? (Y/N)" is better than "Y/N?".
-
Document in script – Add comments mapping ERRORLEVEL to choices:
REM 1=Yes, 2=No, 3=Cancel. -
Consider SET /P for text input – CHOICE is for single-key selection. For free-form input (e.g., filename), use
SET /P variable=. -
Avoid choice in loops without escape – If CHOICE is in a loop, ensure one option exits the loop. Otherwise the user is stuck.
-
Test timeout behavior – Verify /T and /D work as expected. Some environments (e.g., remote sessions) may behave differently.
Troubleshooting Common Issues
CHOICE Not Recognized
Problem: "CHOICE is not recognized as an internal or external command."
Cause: CHOICE was added in Windows 2000. Older systems (Windows 95/98, some NT 4) don't have it. Or PATH doesn't include System32.
Solution: Use full path: C:\Windows\System32\choice.exe. On very old Windows, use SET /P with validation as an alternative.
Prevention: Document Windows version requirement. Use WHERE choice to verify CHOICE is available.
ERRORLEVEL Not Working as Expected
Problem: Batch script branches to wrong label regardless of choice.
Cause: IF ERRORLEVEL order is wrong. IF ERRORLEVEL 1 matches 1, 2, 3, etc. Must check higher values first.
Solution: Reverse the order: check ERRORLEVEL 3, then 2, then 1. Use IF %ERRORLEVEL% EQU 1 for exact match (requires SETLOCAL EnableDelayedExpansion or call a subroutine).
Prevention: Always check highest ERRORLEVEL first. Consider using IF %ERRORLEVEL% EQU N for exact matching when possible.
Timeout Doesn't Work
Problem: /T and /D don't seem to apply; CHOICE waits indefinitely.
Cause: /D must be used with /T. Syntax may be wrong. Some environments (e.g., certain remote sessions) may not support timeout.
Solution: Use both: CHOICE /C YN /T 10 /D N. Ensure no space between /T and the number. Test in a local CMD window first.
Prevention: Verify CHOICE syntax with CHOICE /?. Test timeout in your target environment.
Choice Characters Not Working
Problem: User presses a key but CHOICE doesn't accept it.
Cause: Key not in /C list. Case sensitivity with /CS. Special keys (Enter, Space) may not be in the list.
Solution: Include the key in /C: CHOICE /C YN accepts Y and N. For digits, use CHOICE /C 123. Add Space if desired: CHOICE /C YN " " (Space as choice).
Prevention: Document allowed keys in the /M message. Keep choices simple and common (Y, N, 1, 2, 3).
CHOICE in Redirected/Piped Context
Problem: CHOICE doesn't work when output is redirected or piped.
Cause: CHOICE reads from console. When stdin is redirected, it may not receive keyboard input.
Solution: Run CHOICE in a context where stdin is the console. Avoid using CHOICE in scripts that are fully automated with redirected input.
Prevention: Use CHOICE only in interactive scenarios. For fully automated scripts, use defaults or environment variables instead.
Message with Parentheses Breaks Script
Problem: /M message with ")" causes syntax error.
Cause: Batch parser may interpret ")" as end of IF or FOR block.
Solution: Escape or rephrase: use "Continue (Y/N)?" with quotes. Or avoid parentheses: "Continue? Y or N".
Prevention: Test messages with special characters. Use quotes around the entire /M value.
Related Commands
SET /P – Prompt for Input
SET /P reads a line of input into a variable. Use SET /P for free-form text (e.g., filename, username). Use CHOICE for single-key selection from a fixed set.
When to use CHOICE: Y/N, menu options, single key. When to use SET /P: Text input, numbers, paths.
IF – Conditional Logic
IF branches based on conditions. Use IF ERRORLEVEL N after CHOICE to branch on the user's selection. IF is essential for processing CHOICE results.
When to use CHOICE: Get user selection. When to use IF: Act on that selection.
GOTO – Branching
GOTO jumps to a label. Use with IF ERRORLEVEL after CHOICE to route to different script sections based on user choice.
When to use CHOICE: Get selection. When to use GOTO: Jump to the appropriate handler.
TIMEOUT – Pause
TIMEOUT pauses for a specified number of seconds. Use TIMEOUT for simple delays. Use CHOICE with /T for a delay that can be skipped by user input.
When to use CHOICE /T: Pause with optional user override. When to use TIMEOUT: Simple fixed delay.
PAUSE – Wait for Key
PAUSE waits for any key. Use PAUSE when you just need to wait. Use CHOICE when you need to capture which key was pressed and branch accordingly.
When to use CHOICE: Need to know what user selected. When to use PAUSE: Just need to wait for acknowledgment.
Frequently Asked Questions
What does the CHOICE command do?
CHOICE displays a prompt with a set of allowed keys (e.g., [Y,N]?) and waits for the user to press one. It sets ERRORLEVEL to the 1-based index of the choice (1=first, 2=second, etc.) so batch scripts can branch with IF ERRORLEVEL.
How do I use CHOICE for Yes/No?
Use CHOICE /C YN /M "Your message". Then IF ERRORLEVEL 2 goto no and IF ERRORLEVEL 1 goto yes. Check ERRORLEVEL 2 before 1 because IF ERRORLEVEL N means ">= N".
What is ERRORLEVEL for each choice?
ERRORLEVEL 1 = first character in /C, 2 = second, 3 = third, etc. ERRORLEVEL 0 or 255 = Ctrl+C or abort. Example: /C YN gives ERRORLEVEL 1 for Y, 2 for N.
Can CHOICE have a timeout?
Yes. Use CHOICE /C YN /T 10 /D N for a 10-second timeout with N as default. If the user doesn't press a key in 10 seconds, N is selected automatically.
How do I create a menu with CHOICE?
Use CHOICE /C 1234 /M "1=Option1 2=Option2 3=Option3 4=Exit". Then check ERRORLEVEL 4, 3, 2, 1 in that order and GOTO the appropriate label. Loop back to CHOICE for a persistent menu.
Is CHOICE available on all Windows versions?
CHOICE is available from Windows 2000 onward (including XP, Vista, 7, 8, 10, 11, and Server). It is not on Windows 95/98. Use SET /P as an alternative on very old systems.
Why does IF ERRORLEVEL 1 always run?
Because IF ERRORLEVEL 1 is true when ERRORLEVEL is 1 or higher. So it matches 1, 2, 3, etc. Check higher values first: IF ERRORLEVEL 3, then IF ERRORLEVEL 2, then IF ERRORLEVEL 1.
How do I make CHOICE case-sensitive?
Use the /CS switch: CHOICE /C Ab /CS. Without /CS, A and a are the same. With /CS, they are different choices.
Can I hide the choice list [Y,N]?
Yes. Use /N: CHOICE /C YN /N /M "Press Y or N: ". The list of choices is hidden; only your message is shown.
How do I set a safe default for destructive operations?
Use CHOICE /C YN /T 5 /D N so that if the user doesn't respond in 5 seconds, N (No) is selected. Avoid /D Y for dangerous operations.
What happens if the user presses Ctrl+C?
CHOICE exits and typically sets ERRORLEVEL to 0 or 255 (version-dependent). Your script can check for this and goto an abort or exit label.
Can I use CHOICE in PowerShell?
CHOICE is a CMD executable and works when called from PowerShell: choice /C YN /M "Continue?". For native PowerShell, use Read-Host or $Host.UI.RawUI.ReadKey() for key input.
Quick Reference Card
| Command | Purpose | Example Use Case |
|---|---|---|
CHOICE /C YN | Yes/No prompt | Confirmation |
CHOICE /C YN /M "Continue?" | Custom message | Clear prompt |
CHOICE /C 123 | Numeric menu | Multi-option selection |
CHOICE /C YN /T 10 /D N | Timeout, default N | Unattended safety |
IF ERRORLEVEL 2 goto no | Branch on second choice | Handle No |
IF ERRORLEVEL 1 goto yes | Branch on first choice | Handle Yes |
CHOICE /C YNC | Three options | Yes/No/Cancel |
CHOICE /N /M "Y or N: " | Hide choice list | Clean prompt |
Try the CHOICE Command in Our Simulator
Practice the CHOICE command in our Windows Command Simulator. Run CHOICE /C YN /M "Continue?" and other examples in your browser. Perfect for learning batch script interactivity and testing before deploying to production.
Visit the Commands Reference for a full list of supported Windows CMD commands, including batch scripting and system utilities.
Summary
The CHOICE command enables user selection in Windows batch scripts. Use /C to define choices (e.g., YN, 123), /M for a message, and /T with /D for timeout and default. CHOICE sets ERRORLEVEL to the 1-based index of the selected choice; use IF ERRORLEVEL (checking higher values first) to branch.
Key use cases include confirmation prompts, interactive menus, installation scripts, and maintenance wizards. Always use safe defaults (e.g., /D N) for destructive operations. CHOICE is available from Windows 2000 onward; use SET /P on older systems.
For related tasks, use SET /P for text input, IF for branching, GOTO for labels, and TIMEOUT for simple delays. Master CHOICE to build interactive, user-friendly batch scripts.