要在 Windows 批处理脚本中打印彩色内容,通常的方式是 echo [32mHello World[0m
,但这种方式需要输入特殊字符 ESC
(ASCII 码为 27),我试过 Alt+027 的快捷键却怎么也打不出来这个字符,而且这种方式各个颜色的编码也很不好记,分享一种更简单的方法!
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/write-host
1 | powershell -Command "Write-Host 'This is Black' -ForegroundColor White -BackgroundColor Black" |
其中单引号内是打印的文字内容,ForegroundColor 参数传文字颜色,BackgroundColor 参数传背景颜色,直接传颜色名字,非常方便。