Hide Password typed in Console - Batch scriptBatch File Command Hide PasswordHide keyboard input for password in console via batch fileHow can I pass arguments to a batch file?Split long commands in multiple lines through Windows batch fileWindows batch files: .bat vs .cmd?How to prevent auto-closing of console after the execution of batch fileHow to sleep for five seconds in a batch file/cmdPowerShell says “execution of scripts is disabled on this system.”How to “comment-out” (add comment) in a batch/cmd?java.lang.NoClassDefFoundError when calling the Java function from Powershell ScriptBatch: Set password with special characters to variablePassword set in batch file not taking last character

What is "aligned sequences" and "consensus sequence" in the context of sequence logo? How to compute these?

Why is it "on the inside" and not "in the inside"?

List of visa access for each country

Nested keyval proper parsing

How do I learn to recognise what is worth publishing

How to find the mass density function of a solid given its total mass, its volume and the position of its center of mass?

Why would anyone ever invest in a cash-only etf?

Are there any unpublished Iain M. Banks short stories?

Dual nationality and return to US the day the US Passport expires

Anti-cheating: should there be a limit to a number of toilet breaks per game per player?

What's the importance of the plane hijacking to the plot of Suspiria (2018)?

Examples of simultaneous independent breakthroughs

How many oliphaunts died in all of the Lord of the Rings battles?

Is it okay for me to decline a project on ethical grounds?

Summoning A Technology Based Demon

Exploiting the delay when a festival ticket is scanned

Does dual boot harms laptop battery or reduces it's life?

Why radial coordinate of a particle must decrease continuously once it is inside the Schwarzschild radius?

Why is the Apollo LEM ladder so far from the ground?

Are fretless stringed instruments used mainly for melody?

Why did I lose on time with 3 pawns vs Knight. Shouldn't it be a draw?

Does Wolfram Mathworld make a mistake describing a discrete probability distribution with a probability density function?

How to access HTML input values from Twig and vice versa

How did the Axis intend to hold the Caucasus?



Hide Password typed in Console - Batch script


Batch File Command Hide PasswordHide keyboard input for password in console via batch fileHow can I pass arguments to a batch file?Split long commands in multiple lines through Windows batch fileWindows batch files: .bat vs .cmd?How to prevent auto-closing of console after the execution of batch fileHow to sleep for five seconds in a batch file/cmdPowerShell says “execution of scripts is disabled on this system.”How to “comment-out” (add comment) in a batch/cmd?java.lang.NoClassDefFoundError when calling the Java function from Powershell ScriptBatch: Set password with special characters to variablePassword set in batch file not taking last character






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















Powershell command used in BAT file is eliminating the escape character available in the password specified by user in the console and saving it in a file.



I have a BAT script which prompt the user to specify the password in console. Password should be masked as ***** while user typing it in console. Password will be something like wel!123. This password is saved into variable and finally getting copied to a text file. I got few suggestion use the below code in my BAT file, but the escape character in the actual password( Wel!123) is getting eliminated and saving the password as Wel123 into login.txt file.



SetLocal
set "psCmd=powershell -Command "$pwd = read-host 'Enter Your Password' -
AsSecureString; $BSTR=
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd);
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /F "usebackq delims=" %%# in (`%psCmd%`) do set "pwd=%%#"

@echo %pwd%>>login.txt









share|improve this question



















  • 2





    I recommend doing this entirely in Powershell.

    – UnhandledExcepSean
    Mar 26 at 19:51











  • Can you tell me how to do it

    – user78873
    Mar 26 at 19:57











  • Possible duplicate of Batch File Command Hide Password

    – Squashman
    Mar 26 at 20:11











  • You are missing some stuff in your code. The reason the ^ does not get written to login.txt is because the variable is expanded before execution and assumes you want to use the ^ as an escape character. You either need to escape the escape with string substitution or output the variable with delayed expansion.

    – Squashman
    Mar 26 at 20:15







  • 1





    @mklement0, you are probably correct. I kept focusing on what the user was writing because they kept saying ESCAPE. The exclamation is not an escape.

    – Squashman
    Mar 26 at 20:37

















1















Powershell command used in BAT file is eliminating the escape character available in the password specified by user in the console and saving it in a file.



I have a BAT script which prompt the user to specify the password in console. Password should be masked as ***** while user typing it in console. Password will be something like wel!123. This password is saved into variable and finally getting copied to a text file. I got few suggestion use the below code in my BAT file, but the escape character in the actual password( Wel!123) is getting eliminated and saving the password as Wel123 into login.txt file.



SetLocal
set "psCmd=powershell -Command "$pwd = read-host 'Enter Your Password' -
AsSecureString; $BSTR=
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd);
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /F "usebackq delims=" %%# in (`%psCmd%`) do set "pwd=%%#"

@echo %pwd%>>login.txt









share|improve this question



















  • 2





    I recommend doing this entirely in Powershell.

    – UnhandledExcepSean
    Mar 26 at 19:51











  • Can you tell me how to do it

    – user78873
    Mar 26 at 19:57











  • Possible duplicate of Batch File Command Hide Password

    – Squashman
    Mar 26 at 20:11











  • You are missing some stuff in your code. The reason the ^ does not get written to login.txt is because the variable is expanded before execution and assumes you want to use the ^ as an escape character. You either need to escape the escape with string substitution or output the variable with delayed expansion.

    – Squashman
    Mar 26 at 20:15







  • 1





    @mklement0, you are probably correct. I kept focusing on what the user was writing because they kept saying ESCAPE. The exclamation is not an escape.

    – Squashman
    Mar 26 at 20:37













1












1








1


1






Powershell command used in BAT file is eliminating the escape character available in the password specified by user in the console and saving it in a file.



I have a BAT script which prompt the user to specify the password in console. Password should be masked as ***** while user typing it in console. Password will be something like wel!123. This password is saved into variable and finally getting copied to a text file. I got few suggestion use the below code in my BAT file, but the escape character in the actual password( Wel!123) is getting eliminated and saving the password as Wel123 into login.txt file.



SetLocal
set "psCmd=powershell -Command "$pwd = read-host 'Enter Your Password' -
AsSecureString; $BSTR=
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd);
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /F "usebackq delims=" %%# in (`%psCmd%`) do set "pwd=%%#"

@echo %pwd%>>login.txt









share|improve this question














Powershell command used in BAT file is eliminating the escape character available in the password specified by user in the console and saving it in a file.



I have a BAT script which prompt the user to specify the password in console. Password should be masked as ***** while user typing it in console. Password will be something like wel!123. This password is saved into variable and finally getting copied to a text file. I got few suggestion use the below code in my BAT file, but the escape character in the actual password( Wel!123) is getting eliminated and saving the password as Wel123 into login.txt file.



SetLocal
set "psCmd=powershell -Command "$pwd = read-host 'Enter Your Password' -
AsSecureString; $BSTR=
[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd);
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /F "usebackq delims=" %%# in (`%psCmd%`) do set "pwd=%%#"

@echo %pwd%>>login.txt






powershell batch-file






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 19:35









user78873user78873

187 bronze badges




187 bronze badges










  • 2





    I recommend doing this entirely in Powershell.

    – UnhandledExcepSean
    Mar 26 at 19:51











  • Can you tell me how to do it

    – user78873
    Mar 26 at 19:57











  • Possible duplicate of Batch File Command Hide Password

    – Squashman
    Mar 26 at 20:11











  • You are missing some stuff in your code. The reason the ^ does not get written to login.txt is because the variable is expanded before execution and assumes you want to use the ^ as an escape character. You either need to escape the escape with string substitution or output the variable with delayed expansion.

    – Squashman
    Mar 26 at 20:15







  • 1





    @mklement0, you are probably correct. I kept focusing on what the user was writing because they kept saying ESCAPE. The exclamation is not an escape.

    – Squashman
    Mar 26 at 20:37












  • 2





    I recommend doing this entirely in Powershell.

    – UnhandledExcepSean
    Mar 26 at 19:51











  • Can you tell me how to do it

    – user78873
    Mar 26 at 19:57











  • Possible duplicate of Batch File Command Hide Password

    – Squashman
    Mar 26 at 20:11











  • You are missing some stuff in your code. The reason the ^ does not get written to login.txt is because the variable is expanded before execution and assumes you want to use the ^ as an escape character. You either need to escape the escape with string substitution or output the variable with delayed expansion.

    – Squashman
    Mar 26 at 20:15







  • 1





    @mklement0, you are probably correct. I kept focusing on what the user was writing because they kept saying ESCAPE. The exclamation is not an escape.

    – Squashman
    Mar 26 at 20:37







2




2





I recommend doing this entirely in Powershell.

– UnhandledExcepSean
Mar 26 at 19:51





I recommend doing this entirely in Powershell.

– UnhandledExcepSean
Mar 26 at 19:51













Can you tell me how to do it

– user78873
Mar 26 at 19:57





Can you tell me how to do it

– user78873
Mar 26 at 19:57













Possible duplicate of Batch File Command Hide Password

– Squashman
Mar 26 at 20:11





Possible duplicate of Batch File Command Hide Password

– Squashman
Mar 26 at 20:11













You are missing some stuff in your code. The reason the ^ does not get written to login.txt is because the variable is expanded before execution and assumes you want to use the ^ as an escape character. You either need to escape the escape with string substitution or output the variable with delayed expansion.

– Squashman
Mar 26 at 20:15






You are missing some stuff in your code. The reason the ^ does not get written to login.txt is because the variable is expanded before execution and assumes you want to use the ^ as an escape character. You either need to escape the escape with string substitution or output the variable with delayed expansion.

– Squashman
Mar 26 at 20:15





1




1





@mklement0, you are probably correct. I kept focusing on what the user was writing because they kept saying ESCAPE. The exclamation is not an escape.

– Squashman
Mar 26 at 20:37





@mklement0, you are probably correct. I kept focusing on what the user was writing because they kept saying ESCAPE. The exclamation is not an escape.

– Squashman
Mar 26 at 20:37












2 Answers
2






active

oldest

votes


















1














! characters disappearing is unrelated to PowerShell - it suggests that cmd.exe's enabledelayedexpansion option is in effect, in which case ! serves as a variable-name delimiter - and in which case isolated ! chars. are simply discarded.



As an aside: ! is therefore not an escape character.



You therefore need to:




  • Disable delayed variable expansion with setLocal disabledelayedexpansion:



    • Note that delayed expansion is off by default, so something in your environment must have turned it on.


  • Additionally, as Squashman points out, ^ characters - cmd.exe's escape char. in unquoted strings - would be "eaten", so you must escape all ^ chars. as ^^, which can you do with %pwd:^=^^%.


To put it all together:



@echo off
setLocal disabledelayedexpansion

set "psCmd=powershell -Command "$pwd = read-host 'Enter Your Password' -AsSecureString; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd); [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""

for /F "usebackq delims=" %%# in (`%psCmd%`) do set "pwd=%%#"

echo %pwd:^=^^%>>login.txt





share|improve this answer



























  • Glad to hear it, @user78873.

    – mklement0
    Apr 10 at 13:38


















1














To convert this to PowerShell, you have most of the code already written:



$pwd = Read-Host 'Enter Your Password' -AsSecureString
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd)
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) | Add-Content -Path login.txt





share|improve this answer



























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55365022%2fhide-password-typed-in-console-batch-script%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    ! characters disappearing is unrelated to PowerShell - it suggests that cmd.exe's enabledelayedexpansion option is in effect, in which case ! serves as a variable-name delimiter - and in which case isolated ! chars. are simply discarded.



    As an aside: ! is therefore not an escape character.



    You therefore need to:




    • Disable delayed variable expansion with setLocal disabledelayedexpansion:



      • Note that delayed expansion is off by default, so something in your environment must have turned it on.


    • Additionally, as Squashman points out, ^ characters - cmd.exe's escape char. in unquoted strings - would be "eaten", so you must escape all ^ chars. as ^^, which can you do with %pwd:^=^^%.


    To put it all together:



    @echo off
    setLocal disabledelayedexpansion

    set "psCmd=powershell -Command "$pwd = read-host 'Enter Your Password' -AsSecureString; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd); [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""

    for /F "usebackq delims=" %%# in (`%psCmd%`) do set "pwd=%%#"

    echo %pwd:^=^^%>>login.txt





    share|improve this answer



























    • Glad to hear it, @user78873.

      – mklement0
      Apr 10 at 13:38















    1














    ! characters disappearing is unrelated to PowerShell - it suggests that cmd.exe's enabledelayedexpansion option is in effect, in which case ! serves as a variable-name delimiter - and in which case isolated ! chars. are simply discarded.



    As an aside: ! is therefore not an escape character.



    You therefore need to:




    • Disable delayed variable expansion with setLocal disabledelayedexpansion:



      • Note that delayed expansion is off by default, so something in your environment must have turned it on.


    • Additionally, as Squashman points out, ^ characters - cmd.exe's escape char. in unquoted strings - would be "eaten", so you must escape all ^ chars. as ^^, which can you do with %pwd:^=^^%.


    To put it all together:



    @echo off
    setLocal disabledelayedexpansion

    set "psCmd=powershell -Command "$pwd = read-host 'Enter Your Password' -AsSecureString; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd); [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""

    for /F "usebackq delims=" %%# in (`%psCmd%`) do set "pwd=%%#"

    echo %pwd:^=^^%>>login.txt





    share|improve this answer



























    • Glad to hear it, @user78873.

      – mklement0
      Apr 10 at 13:38













    1












    1








    1







    ! characters disappearing is unrelated to PowerShell - it suggests that cmd.exe's enabledelayedexpansion option is in effect, in which case ! serves as a variable-name delimiter - and in which case isolated ! chars. are simply discarded.



    As an aside: ! is therefore not an escape character.



    You therefore need to:




    • Disable delayed variable expansion with setLocal disabledelayedexpansion:



      • Note that delayed expansion is off by default, so something in your environment must have turned it on.


    • Additionally, as Squashman points out, ^ characters - cmd.exe's escape char. in unquoted strings - would be "eaten", so you must escape all ^ chars. as ^^, which can you do with %pwd:^=^^%.


    To put it all together:



    @echo off
    setLocal disabledelayedexpansion

    set "psCmd=powershell -Command "$pwd = read-host 'Enter Your Password' -AsSecureString; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd); [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""

    for /F "usebackq delims=" %%# in (`%psCmd%`) do set "pwd=%%#"

    echo %pwd:^=^^%>>login.txt





    share|improve this answer















    ! characters disappearing is unrelated to PowerShell - it suggests that cmd.exe's enabledelayedexpansion option is in effect, in which case ! serves as a variable-name delimiter - and in which case isolated ! chars. are simply discarded.



    As an aside: ! is therefore not an escape character.



    You therefore need to:




    • Disable delayed variable expansion with setLocal disabledelayedexpansion:



      • Note that delayed expansion is off by default, so something in your environment must have turned it on.


    • Additionally, as Squashman points out, ^ characters - cmd.exe's escape char. in unquoted strings - would be "eaten", so you must escape all ^ chars. as ^^, which can you do with %pwd:^=^^%.


    To put it all together:



    @echo off
    setLocal disabledelayedexpansion

    set "psCmd=powershell -Command "$pwd = read-host 'Enter Your Password' -AsSecureString; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd); [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""

    for /F "usebackq delims=" %%# in (`%psCmd%`) do set "pwd=%%#"

    echo %pwd:^=^^%>>login.txt






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 26 at 20:37

























    answered Mar 26 at 20:21









    mklement0mklement0

    151k25 gold badges272 silver badges311 bronze badges




    151k25 gold badges272 silver badges311 bronze badges















    • Glad to hear it, @user78873.

      – mklement0
      Apr 10 at 13:38

















    • Glad to hear it, @user78873.

      – mklement0
      Apr 10 at 13:38
















    Glad to hear it, @user78873.

    – mklement0
    Apr 10 at 13:38





    Glad to hear it, @user78873.

    – mklement0
    Apr 10 at 13:38













    1














    To convert this to PowerShell, you have most of the code already written:



    $pwd = Read-Host 'Enter Your Password' -AsSecureString
    $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd)
    [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) | Add-Content -Path login.txt





    share|improve this answer





























      1














      To convert this to PowerShell, you have most of the code already written:



      $pwd = Read-Host 'Enter Your Password' -AsSecureString
      $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd)
      [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) | Add-Content -Path login.txt





      share|improve this answer



























        1












        1








        1







        To convert this to PowerShell, you have most of the code already written:



        $pwd = Read-Host 'Enter Your Password' -AsSecureString
        $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd)
        [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) | Add-Content -Path login.txt





        share|improve this answer













        To convert this to PowerShell, you have most of the code already written:



        $pwd = Read-Host 'Enter Your Password' -AsSecureString
        $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd)
        [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) | Add-Content -Path login.txt






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 26 at 20:05









        AdminOfThingsAdminOfThings

        5,7902 gold badges2 silver badges14 bronze badges




        5,7902 gold badges2 silver badges14 bronze badges






























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55365022%2fhide-password-typed-in-console-batch-script%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

            용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

            155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해