PowerShell - 覆盖用 Write-Host 编写的行 [英] PowerShell - Overwriting line written with Write-Host
问题描述
我正在尝试覆盖用 Write-Host 编写的 PowerShell 中的一行(我有一个循环运行的进程,我想在屏幕上显示更新的百分比).我试图做的是:
I'm trying to overwrite a line in PowerShell written with Write-Host (I have a process that's running in a loop and I want to show percentage updated on the screen). What I've tried to do is this:
Write-Host -NoNewline "`rWriting $outputFileName ($i/$fileCount)... $perc%"
但它不会覆盖该行,而是保留在同一行并附加到该行.
but instead of overwriting the line it stays on the same line and appends to it.
我在这里遗漏了什么?
谢谢
推荐答案
您不能覆盖 Powershell 窗口中的一行. 您可以使用 cls
(Clear-Host) 将窗口清空:
You cannot overwrite a line in a Powershell window. What you can do is blank the window with cls
(Clear-Host):
# loop code
cls
Write-Host "`rWriting $outputFileName ($i/$fileCount)... $perc%"
# end loop
但您真正应该使用的是 Write-Progress
,一个专门为此目的构建的 cmdlet:
But what you should really be using is Write-Progress
, a cmdlet built specifically for this purpose:
# loop code
Write-Progress -Activity "Writing $outputFileName" -PercentComplete $perc
# end loop
有关 Write-Progress
的更多信息,请访问:http://technet.microsoft.com/en-us/library/hh849902.aspx
More on Write-Progress
here: http://technet.microsoft.com/en-us/library/hh849902.aspx
这篇关于PowerShell - 覆盖用 Write-Host 编写的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!