Enable UserID and Password Prompt in VBScript

Published on
25,208 Points
3 Endorsements
Last Modified:
Community Pick
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealing with remote computer administration scripts.

Many of us who are working on VBScripts and automation tasks might have encountered a need for prompting the user to provide the login credentials (Username/Password), to perform some task during script execution. It's not always safe to include the logon credentials as part of the code itself because these files are in a plain text format and anyone can read it.

If you’re running Windows XP or Windows Server 2003, you can use ScriptPW (a COM object found only in those two versions of Windows) to mask passwords from the command line. Here’s a sample script that creates an instance of the ScriptPW.Password object and then uses the StdOut Write method to request that the user enter a password:

Set objPassword = CreateObject("ScriptPW.Password") 
WScript.StdOut.Write "Please enter your password:" 

strPassword = objPassword.GetPassword() 
Wscript.Echo "Your password is: " & strPassword

Open in new window

When you run this script, the message "Please enter your password:" appears on the screen. At that point, the script will pause and wait for you to type in a password; you simply type the password and press ENTER. This line of code will then grab the password and store it in the variable strPassword:

strPassword = objPassword.GetPassword()

Open in new window

For this simple script, we just echo back the password typed in to prove that the keystrokes really were captured. We do this because with the GetPassword() method, the keystrokes you type are not displayed onscreen. In other words, the password gets masked.

But this might not serve as a solution to our problem since there are a few drawbacks with this object.  To illustrate, if you try to run this script just by double-clicking the .vbs file (which includes this code) it won't work. To be able to run this code you need to go to a command prompt and run the script by giving the command CSCRIPT scriptPath\scriptFileName.vbs and this can be very inconvenient.  Another drawback is that it only works on two versions of Microsoft Windows.

By default in VBScript you can't find any functionality which could ask a user to provide the userID and password in a GUI environment.  Hence I found an article with code at the Microsoft Scripting Guy site to get these credentials through a web page. I have tweaked the code, which you can find below, so that one can easily use the code to add this functionality to their script without having to first write a html file to ask for the username and password. I am sharing this code as given below, and hope this will help someone who may need it:

You can save this code in a .vbs file to use it.
'=======================[ ASK Password ]========================================'
Option Explicit
Dim strUserID, strPassword


Sub AskPassword()
Dim htmlPwdCode, objCodeFile, objFileSysObj, objBrowser, strButton

Set objFileSysObj = CreateObject("Scripting.FileSystemObject")

htmlPwdCode = "<SCRIPT LANGUAGE=" & Chr(34) & "VBScript" & Chr(34) & ">" & Chr(13) & _
"Sub RunScript" & Chr(13) & _
"    OKClicked.Value = " & Chr(34) & "OK"& Chr(34) & Chr(13) & _
"End Sub" & Chr(13) & _
"Sub CancelScript" & Chr(13) & _
"    OKClicked.Value = " & Chr(34) & "Cancelled" & Chr(34) & Chr(13) & _
"End Sub" & Chr(13) & _
"Sub Default_Buttons" & Chr(13) & _
"	If Window.Event.KeyCode = 13 Then" & Chr(13) & _
"		btnOK.Click" & Chr(13) & _
"	End If" & Chr(13) & _
"End Sub" & Chr(13) & _
"</SCRIPT>" & Chr(13) & _
"<BODY onkeypress='vbs:Default_Buttons'><center><font size=" & Chr(34) & "2" & Chr(34) & " face=" & Chr(34) & "Arial" & Chr(34) & ">" & Chr(13) & _
"User name:&nbsp;&nbsp;&nbsp;" & Chr(13) & _
"<input type=" & Chr(34) & "text" & Chr(34) & " name=" & Chr(34) & "UserName" & Chr(34) & " size=" & Chr(34) & "30" & Chr(34) & "><br>" & Chr(13) & _
"Password :&nbsp;&nbsp;&nbsp; </font><font face=" & Chr(34) & "Arial" & Chr(34) & ">" & Chr(13) & _
"<input type=" & Chr(34) & "password" & Chr(34) & " name=" & Chr(34) & "UserPassword" & Chr(34) & _
" size=" & Chr(34) & "30" & Chr(34) & "></font></p>" & Chr(13) & _
"<input type=" & Chr(34) & "hidden" & Chr(34) & " name=" & Chr(34) & "OKClicked" & Chr(34) & " size = " & Chr(34) & "20" & Chr(34) & ">" & Chr(13) & _
"<input id=" & Chr(34) & "btnOK" & Chr(34) & " class=" & Chr(34) & "button" & Chr(34) & _
" type=" & Chr(34) & "button" & Chr(34) & " value=" & Chr(34) & " OK " & Chr(34) & _
" name=" & Chr(34) & "ok_button" & Chr(34) & " onClick=" & Chr(34) & "RunScript" & Chr(34) & ">" & Chr(13) & _
"<input id=" & Chr(34) & "btnCancel" & Chr(34) & " class=" & Chr(34) & "button" & Chr(34) & _
" type=" & Chr(34) & "button" & Chr(34) & " value=" & Chr(34) & "Cancel" & Chr(34) & _
" name=" & Chr(34) & "cancel_button" & Chr(34) & " onClick=" & Chr(34) & "CancelScript" & Chr(34) & "></center></BODY>"

Set objCodeFile = objFileSysObj.CreateTextFile("AskPassword.html", True)
objCodeFile.Write htmlPwdCode
Set objCodeFile = Nothing

Set objBrowser = CreateObject("InternetExplorer.Application")

With objBrowser
	.Height = 170
	.Width = 400
	.Top = 200
	.Left = 300
	.StatusBar = True
	.Toolbar = False
	.Resizable = False
	.Navigate CreateObject("Scripting.FileSystemObject").GetParentFolderName(Wscript.ScriptFullName) & "\AskPassword.html"
	.Visible = True
End With

Do Until objBrowser.ReadyState = 4
'wait till page loads'

Do While objBrowser.Document.Body.All.OKClicked.Value = ""
    Wscript.Sleep 50                 

strUserID = objBrowser.Document.Body.All.UserName.Value
strPassword = objBrowser.Document.Body.All.UserPassword.Value
strButton = objBrowser.Document.Body.All.OKClicked.Value


If strButton = "Cancelled" Then
	MsgBox "Operation cancelled, script will now exit!"
	'Credentials accepted for further processing
End If
objFileSysObj.DeleteFile "AskPassword.html", True

Set objBrowser = Nothing
Set objFileSysObj = Nothing
End Sub
'=======================[ GOT Password ]========================================'

Open in new window

1) Copy this code
2) and paste the same into notepad/your vbscript code
3) Put the name of the procedure AskPassword in the code where you want the authentication process to start
4) The values captured for username and password will be stored in global variable names strUserID and strPassword respectively. You can refer to the UserID and password captured using this procedure in the code, after the call to this AskPassword procedure

Please feel free to contact me in case you need any further clarification on how to use this code.

LVL 65

Expert Comment

Thanks Savindra.  A nice streamlined way of obtaining a username and password.
LVL 14

Expert Comment

Good solution! The only drawback is it doesn't work when save to the root drive (e.g. C:\)
LVL 47

Expert Comment

Note to PEs: the title of this article has a typo (Propmt)
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

LVL 65

Expert Comment

>> the title of this article has a typo

So it did.  Thanks aikimark.  Fixed.

Sungenwang, it should work from the root of a drive if you change this:
      .Navigate CreateObject("Scripting.FileSystemObject").GetParentFolderName(Wscript.ScriptFullName) & "\AskPassword.html"

to this
      .Navigate Replace(CreateObject("Scripting.FileSystemObject").GetParentFolderName(Wscript.ScriptFullName) & "\AskPassword.html", "\\", "\")


LVL 14

Expert Comment

nice! will keep in my toolbox...

Expert Comment


I am trying to use this script but when the browser window opens, the script exits.

'The interface is unknown' at line 59

Do Until objBrowser.ReadyState = 4

Can anyone help?

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Join & Write a Comment

This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
Other articles by this author

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month