100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Batch

The Choice Command

Use the built-in choice command to build single-keypress menus, timed prompts, and errorlevel-driven branching in Windows batch scripts.

Variables and Control FlowBeginner7 min readJul 10, 2026
Analogies

What Choice Does

choice displays a prompt and waits for a single keypress from a restricted set of options, then sets errorlevel to the 1-based position of the key pressed, so choice /c YNC waiting for Y, N, or C sets errorlevel to 1, 2, or 3 respectively. This is more robust than set /p for menu selection because choice validates the keypress against the allowed set automatically, rejecting anything else without needing manual validation logic in the script. The default option set, if /c is omitted, is just Y and N, mirroring the classic yes/no confirmation prompt.

🏏

Cricket analogy: An umpire's decision review system offering exactly three buttons -- Out, Not Out, Umpire's Call -- and rejecting any other input is like choice /c ONU, restricting input to a fixed valid set automatically.

batch
@echo off
echo Choose an action:
echo [Y] Yes  [N] No  [C] Cancel
choice /c YNC /n /m "Your choice: "
if errorlevel 3 goto CANCELLED
if errorlevel 2 goto NO
if errorlevel 1 goto YES

:YES
echo You chose Yes.
goto END

:NO
echo You chose No.
goto END

:CANCELLED
echo Cancelled.

:END

When checking choice results with if errorlevel, always test from the highest number down to the lowest (errorlevel 3 before errorlevel 2 before errorlevel 1), because if errorlevel N matches N and anything higher, so testing low-to-high would always match the first branch.

Timeouts, Defaults, and Case Sensitivity

The /t switch adds a timeout in seconds combined with /d to specify a default choice if the user does not respond in time, useful for unattended scripts that should proceed automatically rather than hang forever waiting for input: choice /c YN /t 10 /d Y waits 10 seconds then auto-selects Y. By default choice is case-insensitive, but /cs makes it case-sensitive, distinguishing an uppercase Y from a lowercase y as different valid keys. The /m switch supplies a custom message text shown alongside the option list, and /n hides the auto-generated [Y,N]? hint text that choice normally appends.

🏏

Cricket analogy: A rain-delay decision defaulting to 'continue play' if the umpires do not respond within a set time window is like choice /c YN /t 30 /d Y, auto-selecting a default after a timeout.

Case sensitivity matters most in scripts intended to accept both a shortcut key and a more mnemonic alternative, such as distinguishing lowercase y for a quick yes from uppercase Y reserved for a different, more destructive confirmation, which requires /cs. Custom messages via /m "Delete all files? " replace choice's default generic prompt, producing friendlier, context-specific interfaces without needing a separate echo line before the choice call. Combining /n (no hint text) with /m gives full control over exactly what is displayed, letting a script's prompt look identical across different choice calls throughout a longer, more polished interactive tool.

🏏

Cricket analogy: Distinguishing a quick single-run call of 'y' from a more deliberate double-run call of 'Y' shouted with extra emphasis is like choice /c yY /cs, treating the two cases as genuinely different valid inputs.

choice /c YN /n /m "Overwrite existing file? " combines a custom message with hidden hint text, producing a clean prompt that reads exactly 'Overwrite existing file?' with no extra [Y,N]? suffix appended automatically.

  • choice /c OPTIONS restricts input to a fixed key set and sets errorlevel to the 1-based position of the key pressed.
  • Check choice results with if errorlevel N, testing from the highest number down to the lowest to avoid false matches.
  • /t seconds combined with /d default adds a timeout that auto-selects a default choice for unattended scripts.
  • By default choice is case-insensitive; add /cs to distinguish uppercase and lowercase keys as separate valid options.
  • /m "text" supplies a custom prompt message; /n hides the auto-generated [Y,N]? hint text.
  • The default option set without /c is just Y and N, matching the classic yes/no confirmation pattern.
  • choice validates keypresses automatically, rejecting invalid keys without needing manual validation logic like set /p requires.

Practice what you learned

Was this page helpful?

Topics covered

#Batch#WindowsBatchScriptingStudyNotes#MicrosoftTechnologies#TheChoiceCommand#Choice#Command#Does#Timeouts#StudyNotes#SkillVeris