I am writing a batch file to execute some other programs. In this case I need to prompt for a password. Do I have any way to mask the input text? I don't need to print ******* characters instead of input characters. Linux's Password prompt behavior (Print nothing while typing) is enough.
@echo off SET /P variable=Password : echo %variable% Pause
This will read the input but I can't mask the text using this approach.
Use a single percent sign ( % ) to carry out the for command at the command prompt. Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required.
%%a refers to the name of the variable your for loop will write to. Quoted from for /? : FOR %variable IN (set) DO command [command-parameters] %variable Specifies a single letter replaceable parameter. (set) Specifies a set of one or more files.
Batch files are plain-text files, which means they can be edited as a text file by right-clicking the file and clicking Edit as shown in the picture. Once you've clicked edit, your default text editor opens the file and allows it to be modified.
Yes - I am 4 years late.
But I found a way to do this in one line without having to create an external script; by calling powershell commands from a batch file.
Thanks to TessellatingHeckler - without outputting to a text file (I set the powershell command in a variable, because it's pretty messy in one long line inside a for loop).
@echo off set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^ $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^ [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)"" for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p echo %password%
Originally I wrote it to output to a text file, then read from that text file. But the above method is better. In one extremely long, near incomprehensible line:
@echo off powershell -Command $pword = read-host "Enter password" -AsSecureString ; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt & set /p password=<.tmp.txt & del .tmp.txt echo %password%
I'll break this down - you can split it up over a few lines using caret ^
, which is much nicer...
@echo off powershell -Command $pword = read-host "Enter password" -AsSecureString ; ^ $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; ^ [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt set /p password=<.tmp.txt & del .tmp.txt echo %password%
This article explains what the powershell commands are doing; essentially it gets input using Read-Host -AsSecureString
- the following two lines convert that secure string back into plain text, the output (plaintext password) is then sent to a text file using >.tmp.txt
. That file is then read into a variable and deleted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With