powershellbatch-filecmdutf-8fonts

Graphic CLI changed after run the powershell command in batch file


After the PowerShell code the batch file CLI is a little different, i want to change it back

You can see the font changed and the color changed a little

Before PowerShell command

enter image description here

After PowerShell command

enter image description here

@echo off
echo +==================================================+
echo ^|**********************Login***********************^|
echo +==================================================+
echo.
echo Login
setlocal DisableDelayedExpansion
set /p input=Username:
::powershell command
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 passwords=%%p
)
if %passwords% == 123 goto sucess
exit
END LOCAL
:sucess
cls
echo welcom back %Username%!
echo :)
pause
exit

I see the different

before Powershell command

enter image description here

after the Powershell command

enter image description here


Solution

  • I presume the problem stems from chcp 65001 being in effect, i.e. the UTF-8 code page.

    With code page 65001 in effect, a call to powershell.exe - the CLI of Windows PowerShell - indeed unfortunately exhibits the symptom you describe: the currently selected font is changed to a legacy raster font with limited glyph (character) support.[1]

    The following command demonstrates the problem (run from cmd.exe):

    :: Unexpectedly switches to a raster font.
    :: Note: No longer occurs in PowerShell (Core) 7+, with pwsh.exe
    chcp 65001 & powershell -noprofile -c "'hi'"
    

    You can use one of the following workarounds:


    [1] This applies to calls to powershell.exe from existing console windows, except if those windows are running an interactive powershell.exe session.