Search code examples
batch-filehide

Batch File Command Hide Password


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?


Solution

  • 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     
    ::***********************************