set /p: User Input
set /p makes a batch file interactive. It prints a prompt, waits for the user to type a line and press Enter, and stores that line in a variable:
@echo off
set /p "name=What is your name? "
echo Nice to meet you, %name%!A session looks like this (the user typed Ada):
C:\Users\ada>greet.bat
What is your name? Ada
Nice to meet you, Ada!The playground can't type for you, so keyboard examples on this page are shown as plain sessions. Further down you'll see how set /p reads from a file instead, and those examples are runnable.
The syntax in detail
set /p variable=prompt has two parts:
- everything before
=is the variable name; - everything after
=is the prompt text, printed without a newline so the cursor waits on the same line.
Wrap the whole thing in quotes, as with plain set, so you control the trailing space after the prompt: set /p "age=Age: ". The space before the closing quote separates the prompt from what the user types.
Empty input keeps the old value
If the user just presses Enter, set /p does not change the variable. If it was undefined, it stays undefined; if it had a value, the value survives. Use this to provide a default:
@echo off
set "color=green"
set /p "color=Favourite colour [green]: "
echo You chose %color%.C:\Users\ada>colour.bat
Favourite colour [green]:
You chose green.The flip side: if you ask in a loop and reuse the same variable, an empty answer keeps the previous answer. Clear the variable before each question (set "answer=") and test it with if not defined answer.
Validating input
Users type anything. Typical checks are "not empty", "one of these choices" and "a number":
@echo off
:ask
set "reply="
set /p "reply=Continue? (y/n) "
if not defined reply goto ask
if /i "%reply%"=="y" goto yes
if /i "%reply%"=="n" goto no
echo Please answer y or n.
goto ask
:yes
echo Continuing...
goto :eof
:no
echo Stopped.C:\Users\ada>confirm.bat
Continue? (y/n) maybe
Please answer y or n.
Continue? (y/n) Y
Continuing...if /i makes the comparison case-insensitive, and the quotes around "%reply%" keep if from breaking when the reply is empty or contains spaces. The loop uses goto & Labels.
For a one-key menu, choice is often nicer than set /p: it only accepts the listed keys and returns the position of the key in errorlevel.
@echo off
choice /c ynq /m "Save changes"
if errorlevel 3 echo Quit & goto :eof
if errorlevel 2 echo Not saved & goto :eof
echo SavedC:\Users\ada>save.bat
Save changes [Y,N,Q]?N
Not savedif errorlevel N means "errorlevel is N or higher", so the checks go from the highest value down. See Exit Codes & errorlevel.
Reading the first line of a file
set /p reads from standard input, and standard input can be redirected from a file with <. The variable then receives the first line of the file. This is a classic batch idiom and it runs fine in the playground:
@echo off
> version.txt echo 2.7.1
>> version.txt echo this second line is ignored
set /p version=<version.txt
echo Building version %version%Building version 2.7.1
The prompt part after = is empty here, so nothing extra is printed. > version.txt echo ... is the same as echo ... > version.txt, with the redirection written first so no stray space ends up in the file.
Reading a configuration value works the same way:
@echo off
> port.cfg echo 8080
set "port=80"
if exist port.cfg set /p port=<port.cfg
echo Server will listen on port %port%
del port.cfg
if exist port.cfg (echo still there) else (echo port.cfg removed)Server will listen on port 8080
port.cfg removed
To read every line of a file, use for /f instead; set /p stops after the first one.
Reading several lines with a block
If you redirect a parenthesised block, all set /p commands inside it read from the same input, one line each:
@echo off
> user.txt echo ada
>> user.txt echo Lovelace
>> user.txt echo London
(
set /p first=
set /p last=
set /p town=
) < user.txt
echo %first% %last% lives in %town%.ada Lovelace lives in London.
Prompting without a newline
Because set /p prints its prompt without a line break, it is also a trick to print text without a newline, something echo can't do. Feed it empty input from nul so it doesn't wait:
@echo off
<nul set /p "=Loading"
<nul set /p "=."
<nul set /p "=."
<nul set /p "=."
echo doneLoading... done
The variable name is empty ("=Loading"), so nothing is stored; only the prompt is printed.
Passwords and hidden input
set /p echoes everything the user types. CMD has no built-in way to hide input; scripts that need a password usually call PowerShell:
@echo off
for /f "usebackq delims=" %%p in (`powershell -NoProfile -Command "$s = Read-Host 'Password' -AsSecureString; [Net.NetworkCredential]::new('', $s).Password"`) do set "pass=%%p"Even then, the password sits in a plain environment variable, so prefer a credential store or a real secret manager for anything important.
Comparison with other shells
| Task | CMD | Bash | PowerShell |
|---|---|---|---|
| ask for a line | set /p "x=Name: " |
read -p "Name: " x |
$x = Read-Host 'Name' |
| first line of a file | set /p x=<file |
read -r x < file |
$x = Get-Content file -First 1 |
| single key menu | choice /c yn |
read -n 1 |
$Host.UI.RawUI.ReadKey() |
Learn more: Bash Loops (while read), Batch if, for /f: Parse Text.
Test yourself
The variable color is green and the user presses Enter at set /p "color=Colour: ". What is color now?
- An empty string
- Undefined
- green
- Colour:
What does set /p ver=<version.txt store?
- The whole file
- The first line of the file
- The last line of the file
- The file name
Why is if /i "%reply%"=="y" written with quotes?
- Quotes make the comparison numeric
- They keep the if command valid when the reply is empty or has spaces
- They are required by set /p
- They hide the input