Clear command prompt history from within batch.


  1. Posts : 31
    uSW10
       #1

    Clear command prompt history from within batch.


    I have a batch, running in "Command Prompt", that prompts for a password. For any number of reasons I can't guarantee that the window will close before exposing that data (arrow keys browse history list).

    Is there a way to keep the password out of the list or is there a way to clear the list from within the batch itself?

    Thank you.
      My Computer

  2. Bree's Avatar
    Posts : 18,632
    10 Home x64 (20H2) (10 Pro on 2nd pc)
       #2

    fatkatie said:
    I have a batch, running in "Command Prompt", that prompts for a password. ... Is there a way to keep the password out of the list or is there a way to clear the list from within the batch itself?
    No and Yes.

    You can set the size of the list of remembered commands to zero. This will clear all the history, but will also turn off recording any further history. So you should then set a size for the list. Use the command DOSKEY /LISTSIZE=0 after the line prompting for the password. eg..

    Code:
    @echo off
    set /p PWD="Password? : "
    doskey /listsize=0
    echo .
    Echo You typed %PWD%
    
    REM if you want to turn command history back on, you need to set a list size.
    doskey /listsize=16
      My Computers


  3. Posts : 31
    uSW10
    Thread Starter
       #3

    It DOES work on W7 but doesn't do a thing in W10. Tried some of the other switches; reinstall, insert, but nothing helped.
      My Computer

  4. Bree's Avatar
    Posts : 18,632
    10 Home x64 (20H2) (10 Pro on 2nd pc)
       #4

    fatkatie said:
    It DOES work on W7 but doesn't do a thing in W10. Tried some of the other switches; reinstall, insert, but nothing helped.
    It does work in W10. I tested it before posting. Not sure why it doesn't for you....

    Clear command prompt history from within batch.-image.png
      My Computers


  5. Posts : 31
    uSW10
    Thread Starter
       #5

    mine... 10.0.17763.194 No telling what it is. Does even work at the prompt for me.

    Thanks for the help.
      My Computer

  6. Bree's Avatar
    Posts : 18,632
    10 Home x64 (20H2) (10 Pro on 2nd pc)
       #6

    fatkatie said:
    mine... 10.0.17763.194 No telling what it is. Does even work at the prompt for me. Thanks for the help.
    Confirmed. I've tested that and it doesn't work in 1809 (17763), but did in 1803 (17134).



    Edit: further tests have shown that it can be made to work in 1809. It works as expected if you switch the command prompt to 'legacy console mode'. In 1803 it works in both the legacy and the new console modes. This would appear to be a bug in 1809. For more details on the two modes and how to switch, see....

    Command Prompt improvements in Windows 10 - TechNet Articles - United States (English) - TechNet Wiki


      My Computers


  7. Posts : 31
    uSW10
    Thread Starter
       #7

    I'll give it a shot. More settings...
    I had a work around, not a guarantee, as in: cmd /c batch. After the shell terminates there's nothing left of the shell's
    history.
    Thanks again Bree.
      My Computer


  8. Posts : 31
    uSW10
    Thread Starter
       #8

    This just dawned on me. If I'm to rely on something I would like to know what I have. Is there a way to query the status of a command environment within a batch. It would look something like this:

    if not checkLegecyMode then bail

    The alternative is I check the OS version and leave if problems are known.
      My Computer

  9. Bree's Avatar
    Posts : 18,632
    10 Home x64 (20H2) (10 Pro on 2nd pc)
       #9

    I think your cmd /c approach is the most practical solution, especially as it can be run equally well in Powershell. Don't forget that Powershell has now replaced Command prompt at the default option on the Win+X menu.
      My Computers

  10. lx07's Avatar
    Posts : 5,479
    2004
       #10

    You could use powershell to get the password as secure string so password isn't stored in buffer - just *****.
    Code:
    @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%
    pause
    Comes out like this :
    Code:
    Enter Password: ******
    qwerty
    Press any key to continue . . .
    From here : cmd - Can I mask an input text in a bat file - Stack Overflow
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 20:59.
Find Us




Windows 10 Forums