14

I have this batch file I wrote to open putty and want to make it a universal script for others. The script is as follows

@echo off
::Written by Mark Gulick::
::Today's Date 20150316::

set /p U="Enter Username: "
set /p P="Enter Password: "
set /p DC="Enter DC Number: "
start /d "C:\Program Files (x86)\putty\" PUTTY.EXE %U%@b0%DC%db -pw %P%
pause

I would like to make the password not show up and have tried some areas on here and haven't found one that will work. I might be doing it wrong too. I'm a little rusty on my scripting. Am I missing something or should I use something else other then the set command?

Mark Gulick
  • 143
  • 1
  • 1
  • 5

2 Answers2

13

You can do something like this :

@echo off & setlocal DisableDelayedExpansion
Title %~n0
Mode 50,5 & Color 0E
set /p U="Enter Username : "
Call:InputPassword "Enter Password" P
set /p DC="Enter DC Number: "
setlocal EnableDelayedExpansion
start /d "C:\Program Files (x86)\putty\" PUTTY.EXE !U!@b0!DC!db -pw !P!
pause
::***********************************
:InputPassword
Cls
echo.
echo.
set "psCommand=powershell -Command "$pword = read-host '%1' -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
      [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
        for /f "usebackq delims=" %%p in (`%psCommand%`) do set %2=%%p
)
goto :eof     
::***********************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • 2
    Thanks Hackoo, it worked out great just the way I wanted it to! I like the extra flare you added in. – Mark Gulick Mar 29 '16 at 19:39
  • 3
    I had to add "eol=" to allow a special case if the password contains a semicolon `for /f "usebackq delims= eol=" %%p in (`%psCommand%`) do set %2=%%p` – Gnana Nov 21 '17 at 04:14
  • This is great, thank you!. FYI I tested if the password has a **semicolon**, and/or **apostrophes** and did not run into an issue. It's possible that Windows now takes care of this automatically. – ganjeii Dec 29 '19 at 02:29
8

This post on DOSTips references a post here by MC ND, but I can't find the original, so here it is again. Whenever you want to get a password and mask the input, simply call :getPassword target_variable input_prompt where target_variable is the name of the variable you store the password in and input_prompt is whatever you show the user to prompt them to enter their password.

@echo off
setlocal enabledelayedexpansion
set /p "user_name=Enter username here:"
call :getPassword user_password "Enter password here: "
:: The user's password has been stored in the variable %user_password%

exit /b

::------------------------------------------------------------------------------
:: Masks user input and returns the input as a variable.
:: Password-masking code based on http://www.dostips.com/forum/viewtopic.php?p=33538#p33538
::
:: Arguments: %1 - the variable to store the password in
::            %2 - the prompt to display when receiving input
::------------------------------------------------------------------------------
:getPassword
set "_password="

:: We need a backspace to handle character removal
for /f %%a in ('"prompt;$H&for %%b in (0) do rem"') do set "BS=%%a"

:: Prompt the user 
set /p "=%~2" <nul 

:keyLoop
:: Retrieve a keypress
set "key="
for /f "delims=" %%a in ('xcopy /l /w "%~f0" "%~f0" 2^>nul') do if not defined key set "key=%%a"
set "key=%key:~-1%"

:: If No keypress (enter), then exit
:: If backspace, remove character from password and console
:: Otherwise, add a character to password and go ask for next one
if defined key (
    if "%key%"=="%BS%" (
        if defined _password (
            set "_password=%_password:~0,-1%"
            set /p "=!BS! !BS!"<nul
        )
    ) else (
        set "_password=%_password%%key%"
        set /p "="<nul
    )
    goto :keyLoop
)
echo/

:: Return password to caller
set "%~1=%_password%"
goto :eof
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • For those unfamiliar with batch, could you please provide a full, functional .bat file example for Windows 10 x64 that includes a simple Username prompt and Password prompt that includes "Enter Username here:[next line]" and "Enter Password here:[next line]"? Thank you. I know you kindly explained the variable usage, but I keep getting the dreaded 'instantly-open, instantly-close-without-doing-anything' results that almost always happens to me when I try to write and execute a .bat file. – velkoon Aug 11 '17 at 23:33
  • 1
    @velkoon - Done. Also, it's recommended to open the command prompt and run the script from there instead of double-clicking it. – SomethingDark Aug 12 '17 at 00:58
  • Excellent, thank you! When I first tested it, it still didn't work, just like most .bat files I make. So I did some Googling and figured out you need to save .bat files with ANSI encoding (ASCII) in Notepad.exe. Which has probably been my main issue with trying to create functional .bat files since the beginning. Strange it's not pointed out often. – velkoon Aug 14 '17 at 22:31
  • @velkoon - it's not something that breaks scripts very often unless you're using `for` loops or saving in UTF-8. – SomethingDark Aug 14 '17 at 23:27