Skip to content

Instantly share code, notes, and snippets.

@jalbam
Last active December 8, 2020 19:53
Show Gist options
  • Save jalbam/a24edff03fee1241e98dce8c86f8968e to your computer and use it in GitHub Desktop.
Save jalbam/a24edff03fee1241e98dce8c86f8968e to your computer and use it in GitHub Desktop.
Visual Basic Script (VBS) which asks for the password, being masked (not displaying typed characters), and stores it into a variable (working on Windows 10)
' Visual Basic Script (VBS) which asks for the password, being masked (not displaying typed characters), and stores it into a variable (working on Windows 10).
' * Gist by Joan Alba Maldonado: https://gist.github.com/jalbam/a24edff03fee1241e98dce8c86f8968e
' Asks for the password:
Set WshShell = CreateObject("WScript.Shell")
powerShellCommand = "powershell -noprofile -command " & _
"$passwordEncrypted = Read-Host -assecurestring ""Please, enter your password"";" & _
"$passwordBinary = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($passwordEncrypted);" & _
"$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($passwordBinary);" & _
"write-output $password"
Set executor = WshShell.Exec(powerShellCommand)
executor.StdIn.Close
typedPassword = executor.StdOut.ReadAll
Call WshShell.Run("powershell -noprofile -command Remove-Variable -Name * -ErrorAction SilentlyContinue", 0, TRUE)
' Removes carriage return or new line invisible characters (if any):
If (Right(typedPassword, 2) = vbCrLf OR Right(typedPassword, 2) = vbNewLine) Then
typedPassword = Left(typedPassword, Len(typedPassword) - 2)
End If
' Shows the password typed:
' * NOTE: instead of doing this, do whatever you want with the password stored in 'typedPassword'.
WScript.Echo "[" & typedPassword & "]"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment