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.

Revisions

  1. jalbam revised this gist Dec 8, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion vbs-asks-masked-password
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    ' 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).
    ' * Author: Joan Alba Maldonado
    ' * Gist by Joan Alba Maldonado: https://gist.github.com/jalbam/a24edff03fee1241e98dce8c86f8968e

    ' Asks for the password:
    Set WshShell = CreateObject("WScript.Shell")
  2. jalbam revised this gist Dec 8, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion vbs-asks-masked-password
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ' Visual Basic Script 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).
    ' * Author: Joan Alba Maldonado

    ' Asks for the password:
  3. jalbam created this gist Dec 8, 2020.
    23 changes: 23 additions & 0 deletions vbs-asks-masked-password
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    ' Visual Basic Script which asks for the password, being masked (not displaying typed characters), and stores it into a variable (working on Windows 10).
    ' * Author: Joan Alba Maldonado

    ' 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 & "]"