0

I have a requirement like whenever we execute a test.bat file then it should ask enter password and entered password should be hidden with *****. Is there any simplest way in batch programming for the above requirement?

I have written the below script using PowerShell is it fine/recommended to use PowerShell for this or any other simplest way is available?

@ECHO OFF 
setlocal
set "psCommand=powershell -Command "$pword = read-host 'Enter password:' -AsSecureString ; ^
   $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
      [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /F "usebackq delims=" %%G in (`%psCommand%`) do set password=%%G
echo "%password%"  
endlocal

Also suggest how to terminate the batch file as Ctrl+C is not working?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Aryan
  • 69
  • 8

1 Answers1

0

Give a try with this little example :

@echo off
Title %~n0 by Hackoo 2016
Mode 50,5 & Color 0E
:CreatePassword
cls & Color 0E
setlocal DisableDelayedExpansion
Call :InputPassword "Please choose your password" pass1
Call :InputPassword "Please confirm your password" pass2
setlocal EnableDelayedExpansion
If !pass1!==!pass2! ( Goto:Good ) Else ( Goto:Bad )
::***********************************
: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
::***********************************        
:Good
Color 0B                  
Cls
echo.
echo                     Good password
::TimeOut /T 2 /NoBreak>nul
echo Your password stored as : "!Pass2!" without quotes
pause>nul
Goto :Eof
::***********************************
:Bad
Color 0C
Cls
echo.
echo             Wrong password try again
::TimeOut /T 2 /NoBreak>nul
echo  Press any key to retry again
pause>nul
Goto :CreatePassword
::***********************************

And you can also give a try for this complete example Folder Loker.bat

Edit on 31/03/2016 @ 14:43 Another way in pur batch based on replace command

@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set /P "=Enter a Password:" < Nul
Call :PasswordInput
Echo(Your input was:!Line!
pause
Goto :Eof

:PasswordInput
::Author: Carlos Montiers Aguilera
::Last updated: 20150401. Created: 20150401.
::Set in variable Line a input password
For /F skip^=1^ delims^=^ eol^= %%# in (
'"Echo(|Replace.exe "%~f0" . /U /W"') Do Set "CR=%%#"
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do Set "BS=%%#"
Set "Line="
:_PasswordInput_Kbd
Set "CHR=" & For /F skip^=1^ delims^=^ eol^= %%# in (
'Replace.exe "%~f0" . /U /W') Do Set "CHR=%%#"
If !CHR!==!CR! Echo(&Goto :Eof
If !CHR!==!BS! (If Defined Line (Set /P "=!BS! !BS!" <Nul
Set "Line=!Line:~0,-1!"
)
) Else (Set /P "=*" <Nul
If !CHR!==! (Set "Line=!Line!^!"
) Else Set "Line=!Line!!CHR!"
)
Goto :_PasswordInput_Kbd

And this is another version improved by aGerman

@echo off & setlocal
Title %~n0
Mode 50,5 & Color 9E
Set /P "Pass1=Please choose your Password:" < Nul
call :HInput Pass1
echo Input length is %errorlevel%
setlocal EnableDelayedExpansion
echo Your password is !Pass1!
pause
cls
Set /P "Pass2=Please confirm your Password:" < Nul
call :HInput Pass2
echo Input length is %errorlevel%
echo Your password is !Pass2!
pause
cls
If !Pass1!==!Pass2! (echo the two passwords are the same
) else (echo the two passwords does not match)
pause
goto :eof


:HInput [ByRef_VarName]
:: inspired by Carlos
if "%__HI__%" neq "__HI__" (
  setlocal DisableDelayedExpansion EnableExtensions
  set "CR=" &set "S=" &set "N=0" &set "__HI__=__HI__"
  for /f "skip=1" %%i in ('echo(^|replace ? . /u /w') do if not defined CR set "CR=%%i"
  for /f %%i in ('"prompt $H &for %%b in (1) do rem"') do set "BS=%%i"
)
set "C="
for /f skip^=1^ delims^=^ eol^= %%i in ('replace ? . /u /w') do if not defined C set "C=%%i"
setlocal EnableDelayedExpansion EnableExtensions
if "!CR!"=="!C!" (
  echo(
  if "%~1"=="" (
    echo(!S!
    endlocal &endlocal &exit /b %N%
  ) else (
    if defined S (
      for /f delims^=^ eol^= %%i in ("!S!") do endlocal &endlocal &set "%~1=%%i" &exit /b %N%
    ) else endlocal &endlocal &set "%~1=" &exit /b 0
  )
)
if "!BS!"=="!C!" (
  set "C="
  if defined S set /a "N -= 1" &set "S=!S:~,-1!" &<nul set /p "=%BS% %BS%"
) else set /a "N += 1" &<nul set /p "=*"
if not defined S (
  endlocal &set "N=%N%" &set "S=%C%"
) else for /f delims^=^ eol^= %%i in ("!S!") do endlocal &set "N=%N%" &set "S=%%i%C%"
goto HInput
Community
  • 1
  • 1
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • You have used 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 in your code is there any other best way to mask the entered password any simplest way?? – Aryan Mar 31 '16 at 09:38
  • @Aryan Did you mean you want do it in pur batch and avoid using it with powershell ?? is it your aim ? – Hackoo Mar 31 '16 at 09:49
  • Yes exactly, or you can also recommend some easiest way using batch. – Aryan Mar 31 '16 at 11:15
  • @Aryan Check my last edit ! – Hackoo Mar 31 '16 at 13:41