PowerShell - 覆盖用 Write-Host 编写的行 [英] PowerShell - Overwriting line written with Write-Host

查看:45
本文介绍了PowerShell - 覆盖用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆