Windows 10

Batch File To Get Input From User

In this tutorial, we’ll explore how to capture user input using the set /p command in a batch file. Prompting for user interaction is essential in scripting when you need decisions, confirmations, or dynamic data from the user.

Batch files automate a sequence of DOS (Disk Operating System) instructions, streamlining administrative tasks, system configuration, and user interaction.
 

🔤 Syntax: Capturing User Input

To prompt the user and store their response in a variable, use the following syntax:

set /p VariableName=Prompt Message:

This keeps the script interactive and flexible.
 
 

✅ Example: Prompt for Shutdown Confirmation

The example below demonstrates a simple Yes/No prompt asking the user whether they want to shut down the computer. The input is then conditionally processed.

@echo off
echo DO YOU WANT TO SHUT DOWN YOUR COMPUTER? (y/n)
set /p Input=Enter Yes or No: 

if /I "%Input%"=="y" goto yes
goto no

:yes
shutdown /s
:no
pause

🧾 Sample Output:

DO YOU WANT TO SHUT DOWN YOUR COMPUTER? (y/n)
Enter Yes or No: n
Press any key to continue . . .

🔎 Notes:

  • /I makes the comparison case-insensitive, so “Y” and “y” are treated the same.
  • You can expand this by validating input or supporting multiple options (e.g., y, n, exit).
  • shutdown /s initiates a system shutdown. You can replace it with shutdown /r for a restart, or shutdown /l to log off.
  • To delay the shutdown, you can use: shutdown /s /t 60
🚀 Summary

The set /p command is a powerful tool for creating interactive batch scripts. It’s particularly useful for decision-making logic, user confirmation, or entering custom input values.

Leave a Reply

Your email address will not be published. Required fields are marked *