In Windows XP and Windows 2003 you could use
set oScriptPW = CreateObject(„ScriptPW.Password“)
strPassword = oScriptPW.GetPassword()
to read a password from a batch command script without printing it on the screen.
Unfortunately, the DLL that contains the „ScriptPW.Password“ object, scriptpw.dll, no longer exists on Windows 7 and Windows Server 2008.
As a workaround I developed a tiny C# .NET 2.0 Console application that mimics the behaviour of the „GetPassword“ function call. Following is the complete source of the application:
namespace scriptpw { using System; class Program { static void Main() { var s = string.Empty; while (true) { var i = Console.ReadKey(true); if (i.Key == ConsoleKey.Enter) { break; } else { s += i.KeyChar; } } Console.Out.Write(s); } } }
You can also directly download a compiled executable here.
As a small example usage in a batch command file (CMD), the following two lines read a password into the ‚Host1Password‘ variable:
<nul: SET /P Host1Password=Enter your password: for /f "delims=" %%i in ('scriptpw.exe') do set Host1Password=%%i echo .
To be used later e.g.:
someapp --password %Host1Password%
Hope you like it! 🙂
Thanks for sharing this option. Unfortunately this option has a bug =(. You should test password with character „&“
Regards,
FC
Thankx. I am using Windows7 and it works ok for me.
Didn’t work for me:
C:\Windows\system32>SET /P Host1Password=Enter your password: 0for /F „delims=“ %i in (’scriptpw.exe‘) do set Host1Password
=%i
’scriptpw.exe‘ is not recognized as an internal or external command,
operable program or batch file.
C:\Windows\system32>echo .
.
Thank you very much… 🙂 🙂 it worked for me….
Great!