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
After PowerShell command
@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
after the Powershell command
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:
Run your batch file in Windows Terminal (now ships with Windows; available in the Microsoft Store for older Windows versions) instead of in a legacy console window.
Temporarily switch to a code page other than 65001
, assuming it still supports all the characters you need and, if the caller is capturing or redirecting the output, that it is (later) decoded accordingly; applied to the example above:
chcp 437 & powershell -noprofile -c "'hi'" & chcp 65001
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.
[1] This applies to calls to powershell.exe
from existing console windows, except if those windows are running an interactive powershell.exe
session.