用 PowerShell 做倒计时

每秒刷新同一行的倒计时显示。

Windows终端PowerShell效率低风险
运行该循环从十开始倒计时,按 Ctrl+C 可提前停止。

01命令

命令
$ 10..0 | ForEach-Object { Write-Host "`r$_ seconds remaining " -NoNewline; Start-Sleep -Seconds 1 }

02作用说明

把 10..0 管道给 ForEach-Object,用回车符覆盖同一行写出剩余秒数,再休眠一秒。改变范围或用 while 循环可做更长的计时器。

03风险

风险低风险
使用条件Windows 10/11 with PowerShell 5.1 or PowerShell 7+
!注意: 用 Ctrl+C 中断会让光标停在一行中间;之后运行 Clear-Host 清理控制台。

04撤销

✓无需撤销——该命令不会改变持久状态。

05来源

  • Start-Sleep
    Microsoft Learn · vendor-doc · 2026-09-20
    依据: Pauses execution; used to tick the countdown once per second.
  • Write-Host
    Microsoft Learn · vendor-doc · 2026-09-20
    依据: Writes to the console; -NoNewline overwrites the same line.

最后验证: 2026-09-20 · 分类: 系统与设置

06相关方案