Download ArticleDownload Article

Adding a password protection to a windows .batch 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.
  • Type set/p "pass=>" on a new line in your batch file.
  • On the next line, type if NOT %pass%== passwordhere goto :FAIL, replacing "passwordhere" with the password you want to set.
Method 1
Method 1 of 2:

Writing Your Code

Download Article
  1. How.com.vn English: Step 1 Open Notepad.
    You can open the Notepad application by going to Start, then All Programs, and then Accessories. In Accessories you’ll find Notepad. Windows users can just type "notepad", without quotes, into the start menu and hit enter. The application will open. [1]
  2. How.com.vn English: Step 2 Begin your code with @ echo off.
    This is the line that you will use to start your script. Scripting is writing the code that causes the program to run correctly. This line allows you to start writing text. [2] After you write that, you will copy the code below.
    • :A
    • echo enter password to activate program.
    • set/p "pass=>"
    • if NOT %pass%== passwordhere goto :FAIL
    Advertisement
  3. How.com.vn English: Step 3 Add the finishing touches to end your script.
    At the end of your program, you need to add the following lines:
    • :FAIL
    • echo Invalid password. If you want to write additional text you can. If you want to have time between the first line of text and the second line of text add a line that says 'ping localhost (number)'. This designates the amount of time between each line of text. Put it between the second and third line. If you want your program to run slowly and give people time to use each line of text, make the ping localhost time longer. The pause in between each line of text that shows up from the code would be the ping localhost time. If you want the program to say 'Hello' and then say 'how are you' five seconds later, you would write ping localhost 5
    • goto :end
    • :end
  4. Advertisement
Method 2
Method 2 of 2:

Finishing Up

Download Article
  1. Step 1 Change "passwordhere" to the password of your choice.
    You can choose whatever password you want. It can be as long as you want too. Just remember to keep the quotation marks. [3]
  2. How.com.vn English: Step 2 Save the file with an ending of .bat.
    The default will be .txt, so you’ll have to change this. If you’ve already saved the file just go to file, then save as, and then change the extension to .bat. You might need to check the box saying ‘show extension’ if you don’t see .txt.
  3. How.com.vn English: Step 3 Navigate to Performance and Maintenance.
    You can reach this tab from the Control Panel of your computer. Now go to Scheduled Tasks and navigate to the file that you just created. You can set it to run whenever you want it to – when someone tries to log on, when they try to open a certain file or folder, or any other prompt. [4]
  4. Advertisement

Community Q&A

Search
Add New Question
  • Question
    What if I did not add .bat at the end of codes?
    How.com.vn English: 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?
    How.com.vn English: 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?
    How.com.vn English: 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

      • If you are not sure how to create good .bat files see some of other articles to find out how to make some .bat programs
      • This is a very basic password code. Anyone with knowledge of .bat files will still be able to access it.
      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Thanks for submitting a tip for review!
      Advertisement

      About This Article

      How.com.vn is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 11 people, some anonymous, worked to edit and improve it over time. This article has been viewed 213,476 times.
      How helpful is this?
      Co-authors: 11
      Updated: April 1, 2024
      Views: 213,476
      Categories: Hacks | Windows Passwords
      Thanks to all authors for creating a page that has been read 213,476 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement