Skip to main content

How to Confirm Destructive Actions with a "Are You Sure?" Prompt in Batch Script

In automation, some commands are irreversible. Formatting a drive, deleting a massive database log, or terminating a critical system process can have devastating consequences if done accidentally. A "Confirmation Prompt" is a safety valve that forces a human pause. It ensures that the user is fully aware of the consequences before the script proceeds with a high-risk operation.

This guide will explain how to implement a user-friendly "Are You Sure?" confirmation using native Batch commands.

Method 1: The Basic SET /P Prompt

This is the standard approach for simple scripts. It asks for a single character (Y/N) and checks the input.

@echo off
setlocal enabledelayedexpansion

echo [WARNING] This script will PERMANENTLY delete all files in C:\Temp\Downloads.
echo.

set "confirm="
set /p "confirm=Are you sure you want to proceed? (Y/N): "

if /i "!confirm!"=="Y" (
echo [ACTION] Deleting files...
del /q /s "C:\Temp\Downloads\*.*"
) else (
echo [CANCELLED] Operation aborted by user.
)

pause
endlocal
exit /b 0

Method 2: The CHOICE Command (Standardized)

The choice command is a dedicated tool for creating "Y/N" menus. It is superior to set /p because it only accepts the keys you define, it doesn't require the user to press Enter, and it is impossible for the user to type a "silent" error.

@echo off
setlocal

echo [CRITICAL] You are about to restart the SQL Server.

:: /C defines the choices. /M defines the message.
choice /c YN /m "Proceed with restart?"

:: 1 = Y, 2 = N
if %errorlevel% equ 1 (
echo [ACTION] Restarting service now...
net stop mssqlserver && net start mssqlserver
) else (
echo [ABORT] Script stopped.
endlocal
exit /b 1
)

endlocal
exit /b 0
warning

Choice ErrorLevel Ordering. When using if errorlevel, you must check the highest number first or use if %errorlevel% equ .... Writing if errorlevel 1 will catch both 1 and 2, because the syntax means "If errorlevel is 1 OR GREATER."

Method 3: The "Timeout" Auto-Cancel

For automated maintenance, you might want to give a user 10 seconds to say "No" before the script proceeds automatically.

@echo off
setlocal

echo [SYSTEM] Starting automatic disk cleanup in 10 seconds.
echo Press 'N' now to CANCEL.

choice /c YN /t 10 /d Y /m "Continue?"

if %errorlevel% equ 1 (
echo [AUTO] Running cleanup...
:: (cleanup commands here)
) else (
echo [STOP] User cancelled automation.
pause
endlocal
exit /b 1
)

endlocal
exit /b 0

How to Avoid Common Errors

Wrong Way: Case-Sensitive Checks

Using if "%confirm%"=="Y" will fail if the user types a lowercase y.

Correct Way: Use the /i switch for if statements (Method 1) or let the choice command handle it (Method 2), as choice is case-insensitive by default.

Problem: Prompting during a Remote Task

If your script runs as a Scheduled Task or via a remote CI/CD pipeline, there is no "User" to press Y. The script will hang forever waiting for input.

Best Practice: Use an "Auto-Confirm" flag in your script logic.

if /i "%~1"=="--force" set "confirm=Y"

Best Practices and Rules

1. Clear Consequences

Don't just say "Are you sure?" Say "Are you sure you want to delete 500GB of backup data?" The user should know exactly what they are agreeing to.

2. Default to "No"

For the safest scripts, make "N" the default option. If the user accidentally presses Enter or types something random, the destructive action should be blocked.

3. Log the Decision

If your script is for a server, log that "User [Name] confirmed the deletion of [Folder] at [Time]." This creates a clear audit trail if things go wrong.

Conclusions

Implementing a confirmation prompt is a hallmark of professional-grade scriptwriting. By utilizing the CHOICE command and clear, descriptive warnings, you prevent catastrophic accidents and ensure that your automation is both safe and respectful of the user's intent. This "Stop and Think" layer is essential for any script that performs modifications to critical system data or configurations.