-1

In the current scenario, when I run the batch file, the window prompt to enter the password.When I enter the password, characters are visible in the window prompt. I want to mask password.

As of now, for entering password, I have below code in my batch file.

SET /p v_password=Enter user password:

what code should I embed here to mask the password.

Kaushal Talniya
  • 182
  • 2
  • 5
  • 12

1 Answers1

1

To mask the password; You can use the powershell method like posted here in this question


@echo off
set "psCommand=powershell -C "$pword = read-host 'Enter Password' -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
        [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
echo %password%
pause

Here is an example to show you how to use the powershell command into a batch file :

@echo off
Title %~n0
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(
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
::***********************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70