This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
52} How can I enter a password into a variable without echoing it?
This is a task that is best done with the help of Visual Basic
program which the actual CMD script creates. Note that the resultant
password variable is made available to the CMD script since this is
a CMD, not a VBS FAQ.
@echo off & setlocal enableextensions
:: Build a Visual Basic Script
set vbs_=%temp%\tmp$$$.vbs
set skip=
findstr "'%skip%VBS" "%~f0" > "%vbs_%"
::
:: Prompting without linefeed as in Item #15
echo.|set /p="Password: "
:: Run the script with Microsoft Windows Script Host Version 5.6
for /f "tokens=* delims=" %%a in ('
cscript //nologo "%vbs_%"') do set MyPass1=%%a
::
echo.
echo.|set /p="Retype : "
for /f "tokens=* delims=" %%a in ('
cscript //nologo "%vbs_%"') do set MyPass2=%%a
::
:: Clean up
for %%f in ("%vbs_%") do if exist %%f del %%f
::
:: Demonstrate the result
echo.
if "%MyPass1%"=="%MyPass2%" (
echo The entered password was %MyPass1%
) else (
echo No match)
endlocal & goto :EOF
'
'The Visual Basic Script
Set WshPass = WScript.CreateObject("ScriptPW.Password") 'VBS
Password=WshPass.GetPassWord() 'VBS
WScript.Echo PassWord 'VBS
The output would be someting like
C:\_D\TEST>cmdfaq
Password:
Retype :
The entered password was whatever

The task of entering a password in scripting without revealing it can
also be solved using
PowerShell-aided cmd.
exe command-line.
@echo off & setlocal enableextensions
call :GetPasswordPS "Enter the password" MyPassInCmd_
echo Just testing that the method works
echo Will disappear from the history when the cmd task closes
echo %%MyPassInCmd_%%=%MyPassInCmd_%
endlocal & goto :EOF
:: The indentation is essential to avoid incorrect syntax
:GetPasswordPS
setlocal
for /f "usebackq delims=" %%p in (`powershell -NoProfile -Command ^
"$p = Read-Host '%~1' -AsSecureString;[System.Net.NetworkCredential]::new('', $p).Password"`
) do (set "return_=%%p")
endlocal & set "%2=%return_%" & goto :EOF
The output might be e.g.
C:\_M>C:\_D\TEST\CMDFAQ.CMD
Enter the password: *********
Just testing that the method works
Will disappear from the history when the cmd task closes
%MyPassInCmd_%=Joshua(5)
For more PowerShell-aided cmd.exe command-line solutions see
#powershell.
P.S. If you are familiar with sci-fi movies from the 1980's, you might recognize where
the example password comes from.
You might find also
item #182
of interest.