Skip to main content

How to Prompt User for Input in Batch Script

By default, batch scripts run from top to bottom without any user interaction. To create flexible and interactive scripts that can accept names, paths, or simple choices, you need a way to pause and ask the user for input. The standard, built-in command for this is SET, used with its /P (prompt) switch.

This guide will teach you how to use the SET /P command to get input from a user and store it in a variable. You will learn the critical best practices for handling empty input and how to safely use the data you receive, even if it contains special characters.

The Core Command: SET /P

The SET command is normally used to assign a value to a variable (SET MyVar=Hello). However, the /P switch completely changes its behavior. Instead of assigning a value, it displays a prompt message and waits for the user to type something and press Enter. The text the user types is then assigned to the variable.

The Syntax: SET /P VariableName="Your prompt message: "

  • VariableName: The name of the variable where the user's input will be stored.
  • "Your prompt message: ": The string of text that will be displayed to the user.

Basic Example: Asking for a Name

This is the "Hello, World!" of user input. The script asks for the user's name and then greets them.

@ECHO OFF
SET /P "UserName=Please enter your name: "

ECHO.
ECHO Hello, %UserName%! Welcome.

Example of user itneraction: the script will pause and wait for the user to type.

Please enter your name: David

After the user types "David" and presses Enter, the script continues.

Hello, David! Welcome.

Handling Empty Input (A Critical Step)

A major pitfall with SET /P is how it handles empty input. If the user just presses Enter without typing anything, the variable is left unchanged. If the variable had a value before the prompt, it will keep that old value, which can lead to serious logical bugs.

Example of error in action:

@ECHO OFF
SET "UserInput=DefaultValue"
ECHO The value is currently: %UserInput%

SET /P "UserInput=Please enter a new value (or just press Enter): "
ECHO The value is now: %UserInput%

If the user just presses Enter at the prompt, the output will be:

The value is currently: DefaultValue
Please enter a new value (or just press Enter):
The value is now: DefaultValue

The variable incorrectly retained its old value.

Solution: Always Clear the Variable Before the Prompt

The only robust way to handle user input is to always set the variable to an empty state immediately before the /P switch. This ensures you get a predictable result.

@ECHO OFF
SET "UserInput=DefaultValue"
ECHO The value is currently: %UserInput%

REM --- This is the critical best practice ---
SET "UserInput="
SET /P "UserInput=Please enter a new value (or just press Enter): "

IF "%UserInput%"=="" (
ECHO You entered nothing.
) ELSE (
ECHO You entered: %UserInput%
)

Common Pitfalls and How to Solve Them

Problem: Input Contains Special Characters (&, |, >)

If a user enters a string containing special command characters, it can break your script when you later try to ECHO or use that variable.

Example of script with error: at the prompt Enter your command:, the user types copy a & copy b.

SET /P "Command="
ECHO You entered: %Command%

This will fail because the & will be interpreted as a command separator by the ECHO line.

Solution: Use Delayed Expansion

The safest way to handle potentially "unsafe" user input is to enable delayed expansion and use exclamation marks (!Var!) instead of percent signs.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "Command="
SET /P "Command=Enter your command: "

ECHO You entered: !Command!
note

Delayed expansion (!Command!) displays the variable's content literally, without letting the command processor misinterpret any special characters inside it.

Practical Example: A Confirmation Prompt (Y/N)

This is one of the most common uses for SET /P. The script asks for confirmation before performing a potentially destructive action.

@ECHO OFF
SET "TARGET_FILE=important_data.log"

ECHO WARNING: You are about to delete the file "%TARGET_FILE%".
ECHO This action cannot be undone.
ECHO.

REM --- Clear the variable first ---
SET "Confirm="
SET /P "Confirm=Are you sure you want to continue? (Y/N): "

REM The /I switch makes the comparison case-insensitive.
IF /I "%Confirm%"=="Y" (
ECHO Deleting file...
DEL "%TARGET_FILE%"
ECHO File deleted.
) ELSE (
ECHO Operation cancelled.
)

Conclusion

The SET /P command is the essential tool for making your batch scripts interactive. It's simple to use but requires careful handling to be robust.

For reliable scripts that accept user input:

  • Use the syntax SET /P "VarName=Prompt: ".
  • Always clear the variable (SET "VarName=") immediately before the prompt to correctly handle empty input.
  • Use IF /I for case-insensitive comparisons of user choices (like "Y/N").
  • For handling the user's input safely after you receive it, enable and use Delayed Expansion (!VarName!).