用 PowerShell 显示加载动画

循环切换字符,在等待时显示活动指示。

Windows终端PowerShell效率低风险
运行该片段,控制台中会出现旋转的加载动画。

01命令

命令
$ $chars = "|","/","-","\"; 1..20 | ForEach-Object { Write-Host -NoNewline "`r$($chars[$_ % 4]) Working..."; Start-Sleep -Milliseconds 150 }

02作用说明

循环从四个帧字符数组中取字符,用回车符和 -NoNewline 重写同一行,形成流畅的旋转动画。把耗时任务放进循环体即可当作真正的进度指示。

03风险

风险低风险
使用条件Windows 10/11 with PowerShell 5.1 or PowerShell 7+
!注意: 反斜杠在 PowerShell 数组中需要转义;帧字符应保持单个字符。

04撤销

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

05来源

  • Write-Host
    Microsoft Learn · vendor-doc · 2026-09-20
    依据: Writes to the host; -NoNewline and carriage returns drive the animation.

最后验证: 2026-09-20 · 分类: 开发者

06相关方案