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! 🙂