I need to get authentication credentials from the users within a Windows script but the classic "first Google result" approach:
SET /P USR=Username:
SET /P PWD=Password:
is less than satisfying, so I was wondering if there's let's say an "equivalent" to HTML's input type="password"?
Any comment would be really appreciated, thanks much in advance!
another alternative is my EditV32 (x86) or EditV64 (x64) command-line tools. For example:
-m
means "masked input" and-p
is the prompt. The user's input is stored in the PWD environment variable. You can get it here:http://www.westmesatech.com/editv.html
Another approach is to call
PowerShell
commands from yourBatch
script. Here's an example that configures the logon account of a service:where THE_SERVICE_NAME is the name of the service to configure and THE_ACCOUNT is the logon account.
Then we can use it from a batch script like that:
which is simply calling PowerShell.exe and passing the three commands.
The advantage of this approach is that the majority of Windows installations today include
PowerShell
, so no extra program or script is needed. The drawback is that you will need to either use the password inside thePowerShell
call (like in my example) or store it in an environment variable and then use it from your batch script. I preffer the former because it is more secure and simpler.By judicious use of another tool freely available on Windows, the following two scripts do the job you want.
First, GetPwd.cmd:
Then, GetPwd.vbs:
Explanation:
GetPwd.vbs simply uses the password object to input the password from the user and then print it to standard output (next paragraph will explain why that doesn't show up in the terminal).
GetPwd.cmd is a bit trickier (but command scripts usually are).
The
"<nul: set /p passwd=Password: "
command simply outputs the prompt with no trailing CR/LF - it's a sneaky way to emulate bash's"echo -n"
. It setspasswd
to an empty string as a side effect and doesn't wait for input since it's taking its input from the nul: device.The
"for /f "delims=" %%i in ('cscript /nologo GetPwd.vbs') do set passwd=%%i"
statement is the trickiest bit. It runs the vbscript with no Microsoft advertising (/nologo
), so that the only line output is the password (from the vbscript"Wscript.StdOut.WriteLine strPassword"
.Setting the delimiters to nothing is required to capture input lines with spaces, otherwise you just get the first word. The
"for ... do set ..."
setspasswd
to be the actual password output from the vbscript.Then we echo a blank line (actually terminate the
"Password: "
line) and echo the password so you can verify it works:The scriptpw.dll is available with XP and 2K3 but not necessarily later versions.
Instructions for Vista and presumably Win7 are below, give them a try:
You may use ReadFormattedLine subroutine for all kind of formatted input. For example, the commands below read an username and password of 8 characters each, display asterisks in the screen, and continue automatically with no need to press Enter:
Or in a different way:
In previous example, when the user completed the username, the subroutine display the slash and read the password; if the user delete characters, the slash is also deleted automatically.
This subroutine is written in pure Batch so it does not require any additional program, and it allows several formatted input operations, like read just numbers, convert letters to uppercase, etc. You may download ReadFormattedLine subroutine from Read a line with specific format.
Another alternative is ReadLine.exe. Example: