我有一个自动执行某些程序的Powershell脚本,我在每个程序完成时使用“Write-Host”告诉技术用户该步骤已完成,但我意识到这有点难以阅读和跟踪,所以,是否有一种方法可以输出粗体文本?
提前感谢您。
提前感谢您。
仅仅是文字...
从PowerShell 6及更高版本附带的'ConvertFrom-MarkDown' cmdlet(命令)可以看出,加粗文本实际存在:
(使用默认的PowerShell颜色方案)
('This is **Bold** text' | ConvertFrom-MarkDown -AsVt100EncodedString).VT100EncodedString
但这并不完全公平,因为背后的技巧是调整颜色。知道默认前景颜色是Gray
,而White
只是稍微亮一点,因此看起来更加加粗。
这意味着上述语句类似于:
Write-Host 'This is ' -NoNewline; Write-Host -ForegroundColor White 'Bold ' -NoNewline; Write-Host 'Text'
相关链接:
从 PowerShell 5.1 开始,PowerShell 控制台支持VT转义序列,这些序列可以用于定位和格式化控制台文本。请注意,此功能仅在 Windows 10 及更高版本的操作系统上受支持。
# Enable underline
$esc = [char]27
"$esc[4mOutput is now underlined!"
# Disable underline
$esc = [char]27
"$esc[0mReset"
或者就像之前指出的那样,使用Write-Host,在行内切换文本颜色以获得对比度...
Write-Host -ForegroundColor gray 'This is not bold ' -NoNewline
Write-Host -ForegroundColor White 'This is Bold ' -NoNewline
Write-Host -ForegroundColor gray 'This is not bold'
那就是如果您没有下载或无法下载并使用外部工具。
Write-Host "This text is red" -ForegroundColor red
这将显示蓝色文字在红色背景上:
Write-Host "This text is blue" -ForegroundColor blue -BackgroundColor red
Write-Host (2,4,6,8,10,12) -Separator ", -> " -ForegroundColor DarkGreen -BackgroundColor White
。 - Vivek Kumar Singh