PDF download Download Article
A batch scripting tutorial for beginners and seasoned users alike
PDF download Download Article

Adding password protection to a Windows .bat script isn’t incredibly difficult, but it would be hard to accomplish without any instructions. If you’re willing to take the time to learn then you'll have some password protection in no time.

Password Protecting a Batch File in Windows

  • To protect your .bat file with a password, you'll need to add a few lines to your code.
  • You can add simple password protection or make it more advanced by adding a choice to try again if an incorrect password is typed.
  • To keep other users from opening the .bat file to see the password, hide the file.
  • You can also password-protect a .bat file by adding it to a password-protected archive.
Method 1
Method 1 of 3:

Adding Simple Password Protection

PDF download Download Article
  1. You can open the Notepad application by typing "notepad" in the taskbar search bar and then clicking the application.
  2. This is the line that you will use to start your script. Scripting is writing the code that causes the program to run correctly. The @echo off line keeps the resulting script tidier in the Command Prompt.[1]
    Advertisement
  3. To do this, add the following two lines to your code:[2]
    set "p="
    set /p "p=Type your password: "
    
    • Feel free to change the label "Type your password: ", but remember to leave quotes around it.
  4. To do this, you'll need to use the logical operators "if" and "if not" to check whether the password is correct. Add these two lines to your code:
    if not "%p%"=="pass" goto :FAIL
    if "%p%"=="pass" goto :SUCCEED
    
    • Change "pass" to your desired password. Remember to keep the quotes around it, however.
  5. This will let the user know that they entered the password incorrectly.
    :FAIL
    echo Invalid password.
    goto :END
    
  6. This will allow the user to know that they entered the password correctly.
    :SUCCEED
    echo Correct password. The script will be run.
    goto :END
    
  7. After setting up the password protection, you can write the rest of your code. However, be aware that you cannot use the labels "FAIL," "SUCCEED," or "END" in the rest of your code. If you need to use those labels in your script, go back to the password protection part of the script and change :FAIL, :SUCCEED, and/or :END to something else.[3]
  8. Normally, batch files end abruptly. However, add the following lines at the end of your script to allow the script to pause until you press a button.
    :END
    echo Press any key to exit.
    pause>nul
    
  9. Go to File > Save as… and save your script with any name you like, but be sure to add .bat to the ending to save it as a batch file.
  10. If anything doesn't work, ensure your code matches the full code below.
    @echo off
    set "p="
    set /p "p=Type your password: "
    if not "%p%"=="pass" goto :FAIL
    if "%p%"=="pass" goto :SUCCEED
    
    :FAIL
    echo Invalid password.
    goto :END
    
    :SUCCEED
    echo Correct password. The script will be run.
    goto :END
    
    :END
    echo Press any key to exit.
    pause>nul
    
  11. To keep other users from opening the batch file in Notepad to see the password, you can hide the file. However, note that hidden files can easily be viewed in File Explorer, so this isn't a foolproof way to protect the file. If you need something a little more robust, jump to our method on adding the file to a password-protected archive.
    • To hide your .bat file, right-click it and choose Properties.
    • At the bottom of the "General" tab (the default tab), tick the box next to Hidden.
    • Click OK to save your choice.
      • Note that you won't be able to see your file in File Explorer unless you click the View tab in File Explorer and tick the box next to Show hidden files.
  12. Advertisement
Method 2
Method 2 of 3:

Setting Up Advanced Password Protection

PDF download Download Article
  1. You can open the Notepad application by typing "notepad" in the taskbar search bar and then clicking the application.
  2. This is the line that you will use to start your script. Scripting is writing the code that causes the program to run correctly. The @echo off line keeps the resulting script tidier in the Command Prompt.[4]
  3. To do this, add the following two lines to your code:[5]
    :START
    set "p="
    set /p "p=Type your password: "
    
    • Feel free to change the label "Type your password: ", but remember to leave quotes around it.
  4. To do this, you'll need to use the logical operators "if" and "if not" to check whether the password is correct. Add these two lines to your code:
    if not "%p%"=="pass" goto :FAIL
    if "%p%"=="pass" goto :SUCCEED
    
    • Change "pass" to your desired password. Remember to keep the quotes around it, however.
  5. This will let the user know that they entered the password incorrectly.
    :FAIL
    echo Invalid password. Try again?
    goto :CHOICE
    
  6. This will allow the user to know that they entered the password correctly.
    :SUCCEED
    echo Correct password. The script will be run.
    goto :END
    
  7. If the user fails to enter the correct password, you can use the choice and errorlevel parameters to allow them to try again. Add the following lines to your script:
    :CHOICE
    echo [1]: Yes
    echo [2]: No
    choice /n /c:12
    if errorlevel == 2 goto :END
    if errorlevel == 1 goto :START
    
    • To use errorlevel to loop the user back to the start, you must use integers instead of alphabetical characters.
  8. After setting up the password protection, you can write the rest of your code. However, be aware that you cannot use the labels "START," "FAIL," "SUCCEED," "CHOICE," or "END" in the rest of your code. If you need to use those labels in your script, go back to the password protection part of the script and change :START, :FAIL, :SUCCEED, :CHOICE, and/or :END to something else.[6]
  9. Normally, batch files end abruptly. However, add the following lines at the end of your script to allow the script to pause until you press a button.
    :END
    echo Press any key to exit.
    pause>nul
    
  10. Go to File > Save as… and save your script with any name you like, but be sure to add .bat to the ending to save it as a batch file.
  11. If anything doesn't work, ensure your code matches the full code below.
    @echo off
    :START
    set "p="
    set /p "p=Type your password: "
    if not "%p%"=="pass" goto :FAIL
    if "%p%"=="pass" goto :SUCCEED
    
    :FAIL
    echo Invalid password. Try again?
    goto :CHOICE
    
    :SUCCEED
    echo Correct password. The script will be run.
    goto :END
    
    :CHOICE
    echo [1]: Yes
    echo [2]: No
    choice /n /c:12
    if errorlevel == 2 goto :END
    if errorlevel == 1 goto :START
    
    :END
    echo Press any key to exit.
    pause>nul
    
  12. To keep other users from opening the batch file in Notepad to see the password, you can hide the file. However, note that hidden files can easily be viewed in File Explorer, so this isn't a foolproof way to protect the file. If you need something a little more robust, jump to our method on adding the file to a password-protected archive.
    • To hide your .bat file, right-click it and choose Properties.
    • At the bottom of the "General" tab (the default tab), tick the box next to Hidden.
    • Click OK to save your choice.
      • Note that you won't be able to see your file in File Explorer unless you click the View tab in File Explorer and tick the box next to Show hidden files.
  13. Advertisement
Method 3
Method 3 of 3:

Creating a Password Protected Archive

PDF download Download Article
  1. To easily password-protect your files, you can add them to a password-protected archive with WinRAR. You can get WinRAR from their website.
    • If you already have a different archival program (such as 7-Zip), you can use that instead. However, note that the steps may differ slightly if you're not using WinRAR.
  2. This option will have the WinRAR logo next to it, which looks like a stack of books with a belt around them.
  3. By default, the archive's name will be the same as the file that's in it. However, you can change it if you'd prefer the name to be something different.
  4. By default, WinRAR will create a .rar archive. However, you can choose a .zip archive if you prefer.
  5. This button is in the bottom-right portion of the WinRAR window.
  6. When you're done, click OK.
  7. To use the .bat file, double-click on the ZIP or RAR you just created and enter the password when prompted. Then, double-click the .bat file in the WinRAR window to run it.
    • If you get a popup from WinRAR asking you to buy a license, you can simply click Close to close the window and keep using the program. WinRAR is a form of trialware, sometimes called "beggarware," in that it will ask you to buy a license after the 40-day trial period is up, but you don't actually have to buy one unless you need its advanced features.
  8. Advertisement

Community Q&A

Search
Add New Question
  • Question
    What if I did not add .bat at the end of codes?
    Sunbakedcow
    Sunbakedcow
    Community Answer
    It would save as a .txt file.
  • Question
    This is a nice idea. However, nothing prevents a user from editing the file and seeing the clear text password. What can be done to prevent that?
    Community Answer
    Community Answer
    Hide the password .bat file. Attrib it +s +h. And then call it from another, single line .bat, file. That would make it a bit more difficult to edit.
  • Question
    OF the bat file is opened by using Notepad, it will show the code right?
    Community Answer
    Community Answer
    Yes. This method only does the real batch code if the correct password is entered. Basically, this method only password protects running the program.
See more answers
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement

Tips

Submit a Tip
All tip submissions are carefully reviewed before being published
Name
Please provide your name and last initial
Thanks for submitting a tip for review!

About This Article

Hannah Dillon
Written by:
wikiHow Technology Writer
This article was co-authored by wikiHow staff writer, Hannah Dillon. Hannah Dillon is a Technology Writer and Editor at wikiHow. She graduated with a B.A. in Journalism from North Dakota State University in 2013 and has since worked in the video game industry as well as a few newspapers. From a young age Hannah has cultivated a love for writing and technology, and hopes to use these passions in tandem to help others in the articles she writes for wikiHow. This article has been viewed 226,602 times.
How helpful is this?
Co-authors: 11
Updated: January 22, 2025
Views: 226,602
Categories: Hacks | Windows Passwords
Thanks to all authors for creating a page that has been read 226,602 times.

Is this article up to date?

Advertisement