Friday, March 18, 2011

Hiding Password Input in Batch Files

It is sometimes necessary to ask user a password from a batch script. Unfortunately, batch files don't allow you to hide input with "*"s, like ordinary password forms in Windows or Web. Of course, the easiest way is to leave everything as it is, but much better is to hide the password without showing anything. The following batch script shows you how to perform such hiding of characters:
@echo off
echo hP1X500P[PZBBBfh#b##fXf-V@`$fPf]f3/f1/5++u5>in.com
set /p password=Enter password:
for /f "tokens=*" %%i in ('in.com') do set "password=%%i"
del in.com
echo.
echo The Password is:"%password%"
You can easily change this batch file with help of our award-winning tool Dr.Batcher. Hope you'll find both of them (script and tool) useful.

20 comments:

  1. this doesn't work..

    ReplyDelete
    Replies
    1. @echo off
      echo hP1X500P[PZBBBfh#b##fXf-V@`$fPf]f3/f1/5++u5>in.com
      <nul: set /p password=Enter password:
      for /f "tokens=*" %%i in ('in.com') do set "password=%%i"
      del in.com
      echo.
      echo The Password is:"%password%"
      pause

      Delete
  2. What operating system do you use? In my case everything works fine!

    ReplyDelete
  3. I cant get it to work either,, i just echos back the same password? it also creates and "in" named bat file side process... Anything you see wrong?

    ReplyDelete
  4. 'in.com' is used to hide your password from publishing it on the screen. when you type something, it shows nothing, but when you press return key, it shows the string you have entered.

    ReplyDelete
  5. the problem is with 'set /p password=Enter Password:'
    Instead just use 'echo Enter Password:'
    I have no idea how the strange characters echoed to in.com work with 'for /f "tokens=*"' statement.
    but I can now hide a password request and ftp a file from my pc to a mainframe via a bat file

    ReplyDelete
  6. Hi

    I was trying to run the following from a 64 bit Windows machine .

    @echo off
    echo hP1X500P[PZBBBfh#b##fXf-V@`$fPf]f3/f1/5++u5>in.com
    echo Enter password:
    for /f "tokens=*" %%i in ('in.com') do set "password=%%i"
    del in.com
    echo.
    echo The Password is:"%password%"


    But the password is shown as space in the last line.

    ReplyDelete
    Replies
    1. Here, you echoed the statement "Enter password:" but you never prompted for an input with /p not set the password to anything.
      set /p password="Enter Password:"
      The trick is to make sure no white space is exist, otherwise dos treats it differently.

      Delete
  7. in.com will be a 16-bit executable, which will not run on any 64-bit version of Windows.

    ReplyDelete
    Replies
    1. What is the solution for Windows 7 ?

      Delete
  8. this worked fine to me in windows 7
    :PASSWORD
    cls
    echo.
    echo hP1X500P[PZBBBfh#b##fXf-V@`$fPf]f3/f1/5++u5>in.com
    echo Enter your password:
    for /f "tokens=*" %%i in ('in.com') do set "password=%%i"
    del in.com
    echo.
    if %password%==P@ssword2012 goto ok
    goto ERROR2

    :ok
    bla bla bla....

    :ERROR2
    bla bla bla

    pause

    ReplyDelete
  9. It may work on Win 7, but not on 64 bit version - this is my problem, anyone have a solution for 64 bit?

    ReplyDelete
  10. I had to do the "echo enter password" command instead of using set /p but it works!

    Now ... what is this? Why does this work? Really weird. What is the string if gibberish all about?

    ReplyDelete
  11. hola, estoy tratando de usar este comando pero cuando ejecuto el bat se cierra el cmd. tengo windows 7.

    ReplyDelete
  12. any tips for making a predefined password in a batch file?

    ReplyDelete
  13. Code is working perfectly ok. Thanks

    ReplyDelete
  14. This is unfortunately not working for 64 bit Windows. is there a version for 64 bit windows?

    ReplyDelete
    Replies
    1. it does not work in windows xp?

      Delete
    2. Universal code for 32 and 64 bit Windows systems:
      for /f "tokens=*" %%i in ('cmd /q /v:on /c "set /p p=&if defined p echo !p!"') do echo "%%i"

      Delete
  15. Universal Windows code:
    @echo off
    <nul set /p password=EnterPassword:
    for /f "tokens=* delims=" %%i in ('cmd /q /v:on /c "set /p p=&if defined p <nul set /p =!p!"') do set "password=%%i"
    echo.
    echo The Password is: "%password%"
    pause

    ReplyDelete

Translate