choice - Prompt the user for input

Maintained on

When using the command prompt or batch files, there are many situations where you want to accept user input to branch processing.

However, if you let users type freely, unintended input can cause errors.

This page introduces the basic usage and sample code of the choice command, which accepts only the keys you specify.

What Is the choice Command?

The choice command displays a message prompting the user to press one of the specified keys, and lets you branch processing according to the key that was pressed.

The basic usage of the choice command is as follows:

choice [/c <keys>] [/n] [/cs] [/t <seconds> /d <default key>] [/m <message>]

It can also be used without options, in which case it looks like this:

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>choice
[Y,N]?

The pressed key is stored in the %errorlevel% variable as a number indicating its position among the choices. Details are explained below.

Options of the choice Command

The choice command has the following options:

OptionDescription
/c <keys>Specifies the keys the user can choose from. The default is YN (Y: yes, N: no).
/nDoes not display the choices ([Y,N]?) after the prompt string.
/csMakes the choice keys case-sensitive. By default, they are case-insensitive.
/t <seconds>Number of seconds (0 to 9999) to wait before automatically selecting the default choice if nothing is entered. Must be used with /d.
/d <key>Specifies the key selected on timeout. It must be one of the keys given to /c, and must be used with /t.
/m <string>Specifies the message to display in the prompt.

If /t is not specified, the command waits indefinitely until the user presses a key.

Specific Usage of the choice Command

Branch Processing According to User Input

The most basic usage of the choice command is to branch processing according to user input.

In the following sample code, if the user inputs Y, it displays Yes, and if the user inputs N, it displays No.

@echo off
setlocal

choice /c YN /m "Yes or No?"

if %errorlevel% == 1 (
  echo Yes
) else (
  echo No
)

endlocal

When you run the above batch file, it displays as follows:

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>
Yes or No?[Y,N]?
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

If the user inputs Y, it displays as follows:

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>
Yes or No?[Y,N]? Y
Yes
C:\users\user>

Get the Selection from %errorlevel%

The choice command stores in %errorlevel% not the key itself but the position (starting from 1) of the selected key in the keys given to /c.

For example, with /c YN, selecting Y gives 1 and selecting N gives 2. With /c ABC, A is 1, B is 2 and C is 3.

If the user interrupts with Ctrl + C, the value is 0.

When there are three or more choices, combining it with goto keeps the branching readable.

@echo off
setlocal

choice /c ABC /m "Choose an action. [A:Copy, B:Move, C:Cancel]"

if %errorlevel% == 1 goto copy
if %errorlevel% == 2 goto move
goto end

:copy
echo Running copy.
goto end

:move
echo Running move.

:end
endlocal

The value of %errorlevel% is overwritten when other commands run. Check it right after choice, or save it to a variable before using it.

Display a Message and Choices

You can display a message and choices by combining the /c option and the /m option.

In the following sample code, the user is prompted to choose their favorite fruit, and the selected fruit is displayed.

@echo off
setlocal

choice /c AB /m "Please choose your favorite fruit. [A: Apple, B: Banana]"

if %errorlevel% == 1 (
  echo Apple
) else (
  echo Banana
)

endlocal

When you run the above batch file, it displays as follows:

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>
Please choose your favorite fruit. [A: Apple, B: Banana]? [A,B]? A
Apple
C:\users\user>

Enable Timeout

By combining the /t and /d options, you can automatically select the default choice when there is no input for a certain time.

In the following sample code, the Y/N choices are displayed for 5 seconds, and if no selection is made, N is selected and No is displayed.

@echo off
setlocal

choice /c YN /t 5 /d N /m "Yes or No?"

if %errorlevel% == 1 (
  echo Yes
) else (
  echo No
)

endlocal

When you run the above batch file, if a key is pressed, the process runs as usual, but if nothing is entered for 5 seconds, No is displayed as follows:

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>
Yes or No?[Y,N]? N
No
C:\users\user>

Confirmation Test

Finally, let’s try a confirmation test.

The test is completed on the client side, so no data is sent to the server.

Quiz1

What is the choice command in Command Prompt used for?

回答がサーバーに送信されることはありません
Quiz2

How do you get the result after receiving input from the user with the choice command?

回答がサーバーに送信されることはありません
Quiz3

What is the default prompt message for the choice command?

回答がサーバーに送信されることはありません
#Command Prompt #Batch Files #Command Line #Command #User Input