This time, we will show you how to prompt for a password when double-clicking a batch file (with the .bat extension) in Windows 10 or Windows 11.
Normally, when you run a batch file, it runs as is.
If you run it as an administrator, the User Account Control screen will appear and you can run it by clicking "Yes".
If you use this method to prompt for a password, you will not be able to proceed unless you enter the correct password.
Use this when you do not want to execute a batch file or when you want to execute it after a while.
Setting to prompt for password when executing batch file
To prompt for a password when a batch file is executed, you will need to create a batch file that prompts for a password and edit the registry.
If you operate the registry incorrectly, it may cause problems such as the system being unable to start. Please make a backup in advance using system restore or similar, and proceed at your own risk.
- How to create and restore a system restore point
- How to open and back up the Registry Editor
- Windows 11/10 Registry Basics: Concept, How to Open, and Internal Structure
- How to Take Ownership and Change Permissions in Windows 11 Registry
Creating a batch file to prompt for a password
For instructions on how to create a batch file, please refer to the following page.
What is a batch file? How to create and run a batch file
Creating "pass.bat"
Create a batch file called "pass.bat", enter (copy) the following contents into it, and save it.
@echo off
echo "%*"
:loop
set /p in=パスワードの入力:
if not "%in%"=="123" cls&echo パスワードが違います。正しいパスワードを入力してください。&goto loop
cls&"%*"
Move the created "pass.bat" to any location (in this case, directly under the C drive).
Editing the registry
Press Windows key + R to open Run, type "regedit" and press Enter.
This will open the Registry Editor, so make sure you back up the following keys beforehand:
HKEY_CLASSES_ROOT\batfile\shell
1. Open the HKEY_CLASSES_ROOT\batfile\shell\open\command key and double-click "(Default)" in the right column.
2. The value data is "%1" %*, so change it to the following value and click OK.
"C:\pass.bat" %1
3. Open the HKEY_CLASSES_ROOT\batfile\shell\runas\command key and double-click "(Default)" in the right column.
4. The value data is "%SystemRoot%\System32\cmd.exe /C "%1" %*", so change it to the following value and click OK.
%SystemRoot%\System32\cmd.exe /C "C:\pass.bat" %1
This completes the setup, so close the Registry Editor.
*If you want to require a password for files with the extension ".cmd", open the following key and change the value using the same procedure as for ".bat" files.
HKEY_CLASSES_ROOT\cmdfile\shell\open\command
HKEY_CLASSES_ROOT\cmdfile\shell\runas\command
Try running the batch file
So, let's create a batch file anywhere and run it.
sample:
@echo off
echo You have entered the correct password.
pause
When you run the batch file, you will be prompted for a password.

If you enter an incorrect password and press Enter, the message "The password is incorrect. Please enter the correct password." will be displayed and you will be asked to enter the password again.

Enter the correct password and press Enter to execute the command.

If you run it as an administrator, you will be asked for your password after clicking "Yes" on the User Account Control screen.
How to undo
If you want to revert to the original state, run the backed up registry file.
Points to note
If you run the batch file directly from "Run" or a third-party app, you will be asked for a password, but if you run it using "cmd /c "full path of batch file"" or by specifying the full path from the command prompt, it will run directly without asking for a password.




Comment: