Prompt for password when executing batch file on Win10/Win11

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.

table of contents

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.

To avoid panicking if your PC suddenly breaks down, it's a good idea to have a standard backup software to protect your important photos and data.

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&"%*"

The 123 in if not “%in%”==”123” is the password, so feel free to change it.

Move the created "pass.bat" to any location (in this case, directly under the C drive).

Explorer
Explorer

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

C:\pass.bat is the full path to the batch file "pass.bat" that you created earlier, which prompts you for the password.

Editing strings
Editing strings

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
Editing strings
Editing strings

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.

command prompt
command prompt

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.

command prompt
command prompt

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

command prompt
command prompt

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.

If you found this article helpful, please share it on social media.

Person who wrote this article

Driven by questions arising from my daily PC use and the desire to "do more," I have been pursuing self-study in Windows since around 2008. I am sharing the "aha!" techniques and solutions I discovered through trial and error with the sole purpose of helping you in your PC life.

View profile

Comment:

To comment

[About submissions]
We welcome any questions or information regarding the content of the article.
However, please note that content unrelated to the purpose of the article, criticism of specific individuals or organizations, offensive language,Inappropriate wordsComments containing the above may be deleted or made private without notice at the discretion of the administrator.
Please note that spam may be automatically deleted by anti-spam measures.

CAPTCHA


table of contents