You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell $Profile中设置时段问候语字体颜色的问题求助

解决PowerShell Profile中问候语颜色不生效的问题

你的代码存在两处核心问题导致颜色无法生效,同时还有一处拼写错误,修正后的完整代码如下:

# 先获取当前小时数(若$Hour未提前定义)
$Hour = (Get-Date).Hour

if ($Hour -lt 12) {
    Write-Host "Good Morning Ken!" -ForegroundColor Magenta
}
elseif ($Hour -gt 17) {
    Write-Host "Good Evening Ken!" -ForegroundColor Magenta
}
else {
    Write-Host "Good Afternoon Ken!" -ForegroundColor Magenta
}

错误原因说明:

  • 管道语法错误:你之前把| Write-Host放在if语句块外部,不符合PowerShell语法规则。正确做法是在每个条件分支的代码块内部,直接用Write-Host输出带颜色的内容。
  • 参数拼写错误:你写的-ForgroundColor是笔误,正确参数名是-ForegroundColor(缺少字母e)。
  • 额外修正:原代码中的"Good Eventing"是拼写错误,改为"Good Evening"更准确。

内容的提问来源于stack exchange,提问作者okskinner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 14:15:40