Hide password in Command prompt


@echo off

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=" %%p in (`%psCommand%`) do set password=%%p

echo %password%

This trick reads a password on Windows without echoing it to the screen by calling PowerShell from a batch file and using a SecureString prompt, so the typed characters do not appear in the console.

Masking input is a small but useful courtesy in scripts that ask for credentials. Note that hiding the characters guards against shoulder surfing, not against the value being readable once it is in memory.

Comments

Popular posts from this blog

[Solved] Error: No such keg: /usr/local/Cellar/gcc

[How To] Unfollow Non-followers on Instagram