Home > Software engineering >  Graphic CLI changed after run the powershell command in batch file
Graphic CLI changed after run the powershell command in batch file

Time:01-20

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

CodePudding user response:

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

With code page 65001 in effect, 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.

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 have the following options:

  • Run your batch file in Windows Terminal, available in the Microsoft Store instead of in a legacy console window.

  • You can temporarily switch to a code page other than 65001, assuming it still supports all the characters you need; applied to the example above:

    chcp 437 & powershell -noprofile -c "'hi'" & chcp 65001
    
  • You can switch from Windows PowerShell to PowerShell (Core) 7 , the install-on-demand, cross-platform successor edition. Its CLI, pwsh.exe, no longer exhibits the problem.

  •  Tags:  
  • Related