Skip to content

Instantly share code, notes, and snippets.

@lunarplasma
Last active April 26, 2021 13:14
Show Gist options
  • Select an option

  • Save lunarplasma/2a2f4fa464198e210fcb51fef44fbd3d to your computer and use it in GitHub Desktop.

Select an option

Save lunarplasma/2a2f4fa464198e210fcb51fef44fbd3d to your computer and use it in GitHub Desktop.
Batch File password
:: This batch file demonstrates how to use a powershell command to do password entry. The password entry part
:: is done as a batch file function.
:: As ever, call it with
:: CALL :Func param1, param2
@ECHO OFF
CALL :GET_PASSWORD "Enter password", PASSWORD
ECHO Password was: %PASSWORD%
EXIT /b 0
:: Usage: CALL :GET_PASSWORD prompt output
:: prompt: Text prompt to the user
:: output: Variable to set the password
:GET_PASSWORD
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
@lunarplasma

Copy link
Copy Markdown
Author

Source for the powershell stuff: https://stackoverflow.com/a/20343074/3274353

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment