如何在PowerShell中输出粗体文本?

18
我有一个自动执行某些程序的Powershell脚本,我在每个程序完成时使用“Write-Host”告诉技术用户该步骤已完成,但我意识到这有点难以阅读和跟踪,所以,是否有一种方法可以输出粗体文本?
提前感谢您。

1
不,我建议如果你想以更具视觉刺激的方式突出显示消息,可以尝试使用“-ForegroundColor”/“-BackgroundColor”参数进行调整。 :) - Mathias R. Jessen
1
文档中,您可以使用Write-Host (2,4,6,8,10,12) -Separator ", -> " -ForegroundColor DarkGreen -BackgroundColor White - Vivek Kumar Singh
3个回答

16

仅仅是文字...
从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'

3
第二种方法是一个不错的解决方法,同样适用于v5.1版本。 - Dávid Laczkó

10

相关链接:

控制台虚拟终端序列

从 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'

那就是如果您没有下载或无法下载并使用外部工具。


4
据我所知是不可能的,但您可以更改文本的前景色和背景色。这样您就可以输出不同颜色的文本,以突出显示错误或警告。
这将显示红色文本:
Write-Host "This text is red" -ForegroundColor red

这将显示蓝色文字在红色背景上:

Write-Host "This text is blue" -ForegroundColor blue -BackgroundColor red

更多请参见:https://evotec.xyz/powershell-how-to-format-powershell-write-host-with-multiple-colors/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接