在 PowerShell 中输出彩色文字
列出全部控制台颜色,并用该颜色打印颜色名。
运行该循环,查看 Write-Host 渲染的每种 ConsoleColor。
01命令
命令
$ [Enum]::GetValues([ConsoleColor]) | ForEach-Object { Write-Host $_ -ForegroundColor $_ }02作用说明
Write-Host 接受来自 ConsoleColor 枚举的 -ForegroundColor 和 -BackgroundColor。枚举并逐项传入颜色名既能展示调色板,也便于为状态输出挑选易读配色。
03风险
!注意: 颜色取决于控制台主题;在浅色或深色主题下部分组合对比度较差。
04撤销
✓无需撤销——该命令不会改变持久状态。
05来源
- Write-Host依据: Documents -ForegroundColor and -BackgroundColor.
- ConsoleColor Enumeration依据: Lists the console colors accepted by Write-Host -ForegroundColor.
最后验证: 2026-09-20 · 分类: 开发者